[Haskell-cafe] Separate a string into a list of strings

Brandon Moore brandonm at yahoo-inc.com
Mon Jun 12 19:22:33 EDT 2006


Clifford Beshers wrote:
> Sara Kenedy wrote:
> 
>> Hi all,
>>
>> I want to write a function to separate a string into a list of strings
>> separated by commas.
>>
>> Example:
>> separate :: String -> [String]
>>
>> separate "Haskell, Haskell, and Haskell" = ["Haskell", "Haskell", "and 
>> Haskell"]
>>
>> If anyone has some ideas, please share with me. Thanks.
> 
> Here is a solution using the Posix regex module.
> 
>     Prelude Text.Regex> splitRegex (mkRegex "[ \t]*,[ \t]*") "Haskell,
>     Haskell, and Haskell"
>     ["Haskell","Haskell","and Haskell"]
> 
> This form should work regardless of locale, but appears to be broken, 
> although I expect this is either my fault or that of the underlying 
> Posix library:
> 
>     Prelude Text.Regex> splitRegex (mkRegex "[:space:]*,[:space:]*")
>     "Haskell, Haskell, and Haskell"
>     ["Haskell"," Haskell"," and Haskell"]

Going by man grep, those [:foo:] classes are only special inside a 
character class, otherwise [:space:]* = [aceps:]*.

   Prelude Text.Regex> splitRegex (mkRegex "[[:space:]]*,[[:space:]]*")
   "Haskell, Haskell, and Haskell"
   ["Haskell","Haskell","and Haskell"]

Brandon


More information about the Haskell-Cafe mailing list