Regular expression: matching words between white space

妖精的绣舞 提交于 2019-12-19 07:10:45

问题


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 preceded and followed by a whitespace. If its at the beginning of the string there is no whitespace required before - if its at the end, dont't search for whitespace either.

Example:

"WordA WordB WordC-WordD WordE"

I want to match WordA WordB WordE.

I only came up with overcomplicated way of doing this...

(?<=(?<=^)|(?<=\s))\w+(?=(?=\s)|(?=$))

It seems to me there has to be a simple way for such a simple problem.... I figured i can just start with (?<=\s|^) but that doesnt seem possible because "look-behind requires fixed-width pattern".


回答1:


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.


来源:https://stackoverflow.com/questions/45189706/regular-expression-matching-words-between-white-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!