不以exp结尾的负Lookbehind表达式:'\ w +(?<!exp)\ b'或'\ w + \ b(?<!exp)' [英] Negative Lookbehind expression for not ending with exp : '\w+(?<!exp)\b' or '\w+\b(?<!exp)'
问题描述
对于表达式(?<!foo)
,Negative Lookbehind断言,字符串中当前位置的紧前面不是foo.
现在要匹配不以字符abc
结尾的字符串.
形式表达式是\w+(?<!abc)\b
或\b\w+(?<!abc)\b
.
For expression (?<!foo)
,Negative Lookbehind ,asserts that what immediately precedes the current position in the string is not foo.
Now to match string not ending with characters abc
.
The formal expression is \w+(?<!abc)\b
or \b\w+(?<!abc)\b
.
echo "xxabc jkl" | grep -oP '\w+(?<!abc)\b'
jkl
让我们尝试另一种形式:\w+\b(?<!abc)
.
Let's try another form: \w+\b(?<!abc)
.
echo "xxabc jkl" | grep -oP '\w+\b(?<!abc)'
jkl
它也得到正确的答案.
在我看来,\w+\b(?<!abc)
不是负向后看,也不在(?<!abc)
后面,也不能称为负向后看.
1. \w+\b(?<!abc)
表达式是否为负向后看?
2.为什么\w+\b(?<!abc)
与\w+(?<!abc)\b
具有相同的效果?
It get correct answer too.
In my opinion, \w+\b(?<!abc)
is not a Negative Lookbehind,nothing behind (?<!abc)
,it can't called Negative Lookbehind.
1.Is expression \w+\b(?<!abc)
a Negative Lookbehind?
2.Why \w+\b(?<!abc)
has the same effct as \w+(?<!abc)\b
?
推荐答案
1)这是一个否定的回溯窗口.如果您想匹配其他项目未进行的操作,则使用负向回溯.
?<!abc
是否定后退窗口.因此,仅当进行中的标记不是不是"abc"时才是正确的,这与提供的字符串是正确的:"xxabc jkl"
从技术上讲,空格在进行中"jkl"
因此匹配.
1)It is a negative lookback window. Negative lookback is used if you want to match something not proceeded by something else.
?<!abc
is a negative look back window. So it will only be true if the proceeding tokens are not "abc" which is true with the string provided: "xxabc jkl"
technically, a space is proceeding "jkl"
Therefore it matches.
2)\b
查找单词边界.单词边界适用于两个正则表达式中都存在的空格和字符串结尾.如果您确实希望看到差异,请尝试从输入中删除空格.所以
使用"xxabcjkl"
2) \b
looks for a word boundary. Word boundaries apply for spaces and end of strings which are present in both of your regular expressions. If you really want to see a difference, try taking the space out of the input. So
use "xxabcjkl"
这篇关于不以exp结尾的负Lookbehind表达式:'\ w +(?<!exp)\ b'或'\ w + \ b(?<!exp)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!