我不明白为什么正则表达式 (?<=i:(?>\D*))\d
does not match字符串i:>1
.
我的理解是:
- 在索引 0 处:回顾
i
不匹配 - 在索引 1 处:回顾
i:
不匹配 - 在索引 2 处:回顾
i:(?>\D*)
将匹配i:
但是\d
后向查找将不匹配>
- 在索引 3 处:回顾
i:(?>\D*)
将匹配i:>
和\d
后向查找将匹配1
-> 满足正则表达式
最佳答案
参见Regular Expressions Cookbook: Detailed Solutions in Eight Programming Languages :
.NET allows you to use anything inside lookbehind, and it will actually apply the regular expression from right to left. Both the regular expression inside the lookbehind and the subject text are scanned from right to left.
(?<=i:(?>\D*))\d
模式与 1
不匹配在i:>1
因为原子团(?>\D*)
防止任何回溯到其模式。 i:
(实际上, :
然后 i
被匹配)与 \D*
匹配,然后就没有办法重新匹配 i:
因为原子组不允许回溯。
您还可以看到 (?<=i:(?>[^:\d]*))\d
will match 1
in i:>1
因为在这里,[^:\d]*
匹配除 :
之外的任何字符和数字,因此最多只能达到 i:
和i:
仍然有待匹配。
关于.net - 正向回顾内的原子组如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48085381/