In Scala, I have a regular expression pattern match like this:
val Regex = """(\d{4})/(\d{2})/(\d{2})""".r
val Regex(year, month, day) = "2013/01/06"
The result is:
year: String = 2013
month: String = 01
day: String = 06
How can I accomplish a similar result in Haskell? In other words, can I match a regular expression containing groups and assign the groups to identifiers?
This works for me:
Prelude Text.Regex.Posix> "2013/01/06" =~ "([0-9]+)/([0-9]*)/([0-9]*)" :: (String,String,String,[String])
("","2013/01/06","",["2013","01","06"])
(ghci 7.4.2 on OS X)
Expanding on Chris's answer, the following works and is similar to my Scala version:
ghci> :m +Text.Regex.Posix
ghci> let (_, _, _, [year, month, day]) ="2013/01/06" =~ "([0-9]+)/([0-9]*)/([0-9]*)" :: (String,String,String,[String])
ghci> year
"2013"
ghci> month
"01"
ghci> day
"06"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With