You can do almost that with view patterns!
{-# LANGUAGE ViewPatterns #-}
import Text.Regex.Posix
-- Helper
pat :: String -> String -> [[String]]
pat p s = s =~ p
-- Function with matching
foo :: String -> String
foo (pat "foo(bar|baz)" -> [[_,x]]) = x
foo _ = "no!"
main :: IO ()
main = do
print $ foo "foobar"
print $ foo "foobaz"
print $ foo "yes?"
import Text.Regex.Posix
-- Helper
pat :: String -> String -> [[String]]
pat p s = s =~ p
-- Function with matching
foo :: String -> String
foo (pat "foo(bar|baz)" -> [[_,x]]) = x
foo _ = "no!"
main :: IO ()
main = do
print $ foo "foobar"
print $ foo "foobaz"
print $ foo "yes?"
The above code will print bar baz no! . Have fun!
11 comments:
The `rex` library on Hackage makes it even easier, using quasi-quoting.
Really cool dude! Thanks for this one.
the pattern was likely easy done other i saw it was nice.
first look its really rough to know each symbols and sequence of the pattern but its much easy to try.
What programming language you use in this blog?
I'm glad i saw your blog. Thanks for the information. Much appreciated it if you could do some video for it.
i will try this code see the output, thanks!
Is this a C language? because of the word print.
@great site.... yes, I think it's C++
I remember my college days learning this subject, I missed those days
Good post thanks for share information.
SRI ANNAPOORNESHAWARI ASTROLOGY CENTER.Best Astrologer In Nebraska
Post a Comment