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

Clifford Beshers clifford.beshers at linspire.com
Mon Jun 12 19:01:09 EDT 2006


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"]


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org//pipermail/haskell-cafe/attachments/20060612/c5848f11/attachment.htm


More information about the Haskell-Cafe mailing list