Golang regexp vs Grep


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

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

1 Like

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

Thank you very much!

1 Like