doulin6088 2018-12-20 15:44
浏览 1046
已采纳

如何使用Golang正则表达式查找完全匹配的单词?

I am trying to find words in a large text file. The MatchString method doesn't inherently look for exact word match, rather it looks for the pattern as it should.

How does one check for exact word match using regexp package in Golang? I have tried some regex'es i found on SO, but it didn't work.

  • 写回答

1条回答 默认 最新

  • dongqiao6445 2018-12-20 16:22
    关注

    Use the zero-length word boundry sequence \b: https://play.golang.org/p/-f0KEKb2EbF

    regexp.MatchString("\\bword\\b", test)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?