[Haskell-beginners] how to check the format of a string ?

Frerich Raabe raabe at froglogic.com
Sun Feb 22 19:38:38 UTC 2015


On 2015-02-22 19:50, Roelof Wobben wrote:
> Now im facing this problem.
> 
> There is a log file of strings.
> 
> Most of them are of this format   char  Number [chars]
> 
> What is the best way to check if the string has this format ?

For this particular format I'd go for plain pattern matching and guards. 
Something like

   import Data.Char (isLetter, isDigit)

   isValid :: String -> Bool
   isValid s = go (words s)
     where
       go ([a]:b:_) = isLetter a && all isDigit b
       go _         = False

This will accept lines starting with a letter followed by some number and 
then (optionally!) some more text.

-- 
Frerich Raabe - raabe at froglogic.com
www.froglogic.com - Multi-Platform GUI Testing


More information about the Beginners mailing list