PCRE matching whole words in a string

后端 未结 2 747
野趣味
野趣味 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.

提交回复
热议问题