正则表达式:空格之间匹配单词 [英] Regular expression: matching words between white space
问题描述
我正在尝试使用python中的正则表达式做一些相当简单的事情……那至少是我的想法.
我想做的是匹配字符串中的单词(如果在字符串之前和之后是空格).如果它在字符串的开头,则之前不需要空格-如果在它的末尾,也不要搜索空格.
示例:
"WordA WordB WordC-WordD WordE"
我要匹配WordA WordB WordE
.
我只是想出了一种过于复杂的方式...
(?<=(?<=^)|(?<=\s))\w+(?=(?=\s)|(?=$))
在我看来,必须有一种解决这种简单问题的简单方法.
我以为我可以从(?<=\s|^)
开始,但这似乎是不可能的,因为向后看需要固定宽度的样式".
您似乎在Python中工作,因为(?<=^|\s)
在PCRE,Java和Ruby中完全有效(并且.NET regex支持无限宽的后向模式).>
使用
(?<!\S)\w+(?!\S)
它将匹配1个或多个用空格或字符串开头/结尾括起来的单词字符.
请参见 regex演示.
模式详细信息:
-
(?<!\S)
-当引擎在当前位置的左侧立即找到一个非空白字符时,反向查找失败,导致匹配失败 -
\w+
-1个或多个单词字符 -
(?!\S)
-负前瞻,一旦引擎在当前位置右侧立即找到非空白字符,则匹配失败.
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".
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.
这篇关于正则表达式:空格之间匹配单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!