问题描述
debian@wifi:~$ echo "348dfgeccvdf" | grep -oP "\d+(?=\w{6,10})"
348
debian@wifi:~$ echo "348dfgeccvdf" | grep -oP "(?=\w{6,10})\d+"
348
对于\d+(?=\w{6,10})
,这是标准的正向向前表达.
正如WiktorStribiżew在帖子中所说的那样
位置和否定后视
负向后搜索语法以(?<! and ends with the unescaped )
开头.不管它出现在图案的开始,中间还是结尾,它都不会成为负面的向后看.
位置和整个前瞻(后向)表达之间可能没有关系.
For \d+(?=\w{6,10})
,it is the standard positive look ahead expression.
As Wiktor Stribiżew say in the post
position and negative lookbehind
The negative lookbehind syntax starts with (?<! and ends with the unescaped )
. Whether it appears at the start, middle or end of the pattern, it does not stop from being a negative lookbehind.
Maybe it's no realtionship between position and whole lookahead(lookbehind) expression.
根据相同的逻辑推论正面的展望,例如Wiktor Stribiewew对背后的负面展望说:
To infer for positive look ahead according to the same logic ,such as Wiktor Stribiżew say for negative lookbehind:
"\d+(?=\w{6,10})" == "(?=\w{6,10})\d+"
他们俩都对未来充满信心.
Both of them are positive look ahead.
现在这是另一个示例:
echo "adabc adabb" |grep -oP "ad(?=abc)"
ad
echo "adabc adabb" |grep -oP "(?=abc)ad"
为什么"ad(?=abc)"
不等于"(?=abc)ad"
?
如何解释相同结构的表达式(?=\w{6,10})\d+ and (?=abc)ad
?
Why "ad(?=abc)"
is not equal to "(?=abc)ad"
?
How to explain the same structure expression (?=\w{6,10})\d+ and (?=abc)ad
?
推荐答案
ad(?=abc)
解释ad
后跟abc
,而(?=abc)ad
与任何输入字符串都不匹配.导致期望'跟随abc'(?=abc)
,但在正向超前后定义ad
! .模式应类似于此(?=abc)\w+
,以与此输入字符串abcad
ad(?=abc)
explains ad
followed by abc
whereas (?=abc)ad
will not match with any input string. Cause expecting 'followed by abc' (?=abc)
but defining ad
after positive lookahead ! . Pattern should be like this (?=abc)\w+
to match with this input string abcad
(?=\w{6,10})\d+
对于任何输入字符串,此模式也将与此(?=abc)ad
不匹配.模式应该是这样的:
(?=\w{6,10})\d+
this pattern also will not match as this (?=abc)ad
for any input string. Pattern supposed to be like this :
(?=\w{6,10})\w+
和(?=abc)\w+
.
这篇关于如何解释相同结构的表达式`(?= \ w {6,10})\ d +和(?= abc)ad?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!