PHP preg_match to find whole words

后端 未结 2 1970
忘掉有多难
忘掉有多难 2020-11-28 09:26

I\'m quite new to regular expressions. Could you help me to create a pattern, which matches whole words, containing specific part? For example, if I have a text string \"Per

相关标签:
2条回答
  • 2020-11-28 10:11
    preg_match('/\b(express\w+)\b/', $string, $matches); // matches expression
    preg_match('/\b(\w*form\w*)\b/', $string, $matches); // matches perform,
                                                         // formation, unformatted
    

    Where:

    • \b is a word boundary
    • \w+ is one or more "word" character*
    • \w* is zero or more "word" characters

    See the manual on escape sequences for PCRE.


    * Note: although not really a "word character", the underscore _ is also included int the character class \w.

    0 讨论(0)
  • 2020-11-28 10:13

    This matches 'Perform':

    \b(\w*form\w*)\b
    

    This matches 'expression':

    \b(\w*express\w*)\b
    
    0 讨论(0)
提交回复
热议问题