Your first example works for me:
import Text.Regex.Posix function :: String -> String function arg | arg =~ "pattern1" = "1" | arg =~ "pattern2" = "2" | otherwise = "3"
I think your IsString error is related to an overloaded string literal extension. Try disabling this, or try using explicit String strings:
function :: String -> String function arg | arg =~ ("pattern1"::String) = "1" | arg =~ ("pattern2"::String) = "2" | otherwise = "3"
Too loud? You can push the crack in the last lines.
function2 :: String -> String function2 arg | arg =~ s"pattern1" = "1" | arg =~ s"pattern2" = "2" | otherwise = "3" where s :: String -> String s = id
Do you need subgroup matching?
function3 :: String -> String function3 arg | [_,x]:_ <- arg =~ s"pat(t*)ern1" = "matched group: " ++ x -- ...
In function3 "patttern1" variable x will be bound to "tt" . On function3 "xyz" test will fail and the next branch will be checked.
chi
source share