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!
The `rex` library on Hackage makes it even easier, using quasi-quoting.
ReplyDeleteReally cool dude! Thanks for this one.
ReplyDeletethe pattern was likely easy done other i saw it was nice.
ReplyDeletefirst look its really rough to know each symbols and sequence of the pattern but its much easy to try.
ReplyDeleteWhat programming language you use in this blog?
ReplyDeleteI'm glad i saw your blog. Thanks for the information. Much appreciated it if you could do some video for it.
ReplyDeletei will try this code see the output, thanks!
ReplyDeleteIs this a C language? because of the word print.
ReplyDelete@great site.... yes, I think it's C++
ReplyDeleteI remember my college days learning this subject, I missed those days
ReplyDeleteGood post thanks for share information.
ReplyDeleteSRI ANNAPOORNESHAWARI ASTROLOGY CENTER.Best Astrologer In Nebraska