Reputation: 2814
How am I supposed, say, to create a list of matches:
> "aa2gg3tt41u" =~ "\\d+" :: [String]
produces an error no matter how I fool with the type annotation. I expect something like ["2","3","41"]
.
Upvotes: 0
Views: 363
Reputation: 105915
You want to use instance RegexLike a b => RegexContext a b [[b]]
:
> "aa2gg3tt41u" =~ "[0-9]+" :: [[String]]
[["2"],["3"],["41"]]
I hear you say, "why the [[String]]
?" Well, keep in mind that regexes support grouping:
> "aa2gg3tt41u" =~ "([a-z]+)[0-9]" :: [[String]]
[["aa2","aa"],["gg3","gg"],["tt4","tt"]]
The first element in a list will always be the complete match, followed by the sub-matches. If you're just interested in the complete match, use map head
or provide your own operator:
(?=~) :: String -> String -> [String]
str ?=~ re = map head (str =~ re)
Upvotes: 2