Golang regexp vs Grep


(Che) #1

Hello,

I’m trying to use the regexp package to parse the dictionary “british-english” (/usr/share/dict/british-english on Linux system) but unfortunately my code does not seem to do the correct matching. When I use the pattern ^cat$ I get an empty slice whereas egrep returns cat. I have used both ReadString('\') or Read() to read data from the file, there is no difference. Here the complete code https://play.golang.org/p/1FzeuYG6It8


(Johan Dahl) #2

Hi. You have to set the multiline flag otherwise will cat$ just match if the whole text ends in cat

See this example https://play.golang.org/p/f-0sdcrnBxA


(Johan Dahl) #3

PS you could make your program slightly faster if you read the whole file into text in with https://golang.org/pkg/io/ioutil/#ReadFile instead of line by line


(Che) #4

Thank you very much!