Regular expression: matching words between white space

后端 未结 1 764
野性不改
野性不改 2021-01-12 22:19

Im trying to do something fairly simple with regular expression in python... thats what i thought at least.

What i want to do is matching words from a string if its

相关标签:
1条回答
  • 2021-01-12 23:01

    You seem to work in Python as (?<=^|\s) is perfectly valid in PCRE, Java and Ruby (and .NET regex supports infinite width lookbehind patterns).

    Use

    (?<!\S)\w+(?!\S)
    

    It will match 1 or more word chars that are enclosed with whitespace or start/end of string.

    See the regex demo.

    Pattern details:

    • (?<!\S) - a negative lookbehind that fails the match once the engine finds a non-whitespace char immediately to the left of the current location
    • \w+ - 1 or more word chars
    • (?!\S) - a negative lookahead that fails the match once the engine finds a non-whitespace char immediately to the right of the current location.
    0 讨论(0)
提交回复
热议问题