PCRE matching whole words in a string

后端 未结 2 748
野趣味
野趣味 2021-01-18 15:01

I\'m trying to run a regexp in php (preg_match_all) that matches certain whole words in a string, but problem is that it also matches words that contain only pa

相关标签:
2条回答
  • 2021-01-18 15:28

    If you really want to make sure you only get your words and not words that contain them, then you can use word boundary anchors:

    /\b(testable|string)\b/
    

    This will match only a word boundary followed by either testable or string and then another word boundary.

    0 讨论(0)
  • 2021-01-18 15:44

    You don't want a character class with [], you just want to match the words:

    /testable|string/

    0 讨论(0)
提交回复
热议问题