Regular expression: matching words between white space

后端 未结 1 765
野性不改
野性不改 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

    (?

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

    See the regex demo.

    Pattern details:

    • (? - 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)
提交回复
热议问题