I'm trying to make a simple Haskell program that will take any line that looks like someFilenameHere0035.xml and returns 0035. My sample input file, input.txt, would look like this:
someFilenameHere0035.xml
anotherFilenameHere4465.xml
And running: cat input.txt | runhaskell getID.hs should return:
0035
4465
I'm having so much difficulty figuring this out. Here's what I have so far:
import Text.Regex.PCRE
getID :: String -> [String]
getID str = str =~ "([0-9]+)\\.xml" :: [String]
main :: IO ()
main = interact $ unlines . getID
But I get an error message I don't understand at all:
• No instance for (RegexContext Regex String [String])
arising from a use of ‘=~’
• In the expression: str =~ "([0-9]+)\\.xml" :: [String]
In an equation for ‘getID’:
getID str = str =~ "([0-9]+)\\.xml" :: [String] (haskell-stack-ghc)
I feel like I'm really close, but I don't know where to go from here. What am I doing wrong?
grep -o '[0-9]\+\.xml' | sed 's/.xml//'seems to work, and you could probably do it with a singlesedcommand if you don't mind it looking a bit less comprehensible.