量词范围在后面不起作用 [英] Quantifier range not working in lookbehind
问题描述
好的,所以我正在一个项目中,我需要一个可以匹配*的正则表达式,后跟1-4个空格或制表符,然后再跟一行文本.现在,我在后面查找后使用.*进行测试.但是我可以让它明确匹配1、2或4个空格/制表符,但不能匹配1-4.我正在针对以下代码块
Okay so I'm working on a project where I need a regex that can match a * followed by 1-4 spaces or tabs and then followed by a row of text. Right now I'm using .* after the lookbehind for testing purposes. However I can get it to match explicitly 1, 2, or 4 spaces/tabs but not 1-4. I'm testing against the following block
* test line here
* Second test
* Third test
* Another test
这是我正在测试的两个模式(?<=(\*[ \t]{3})).*
,它们按预期方式工作并匹配第二行,如果我将1替换为3,将1或2或4替换为2,则相同,形成以下内容模式(?<=(\*[ \t]{1,4})).*
,它不再与任何行匹配,老实说,我不明白为什么.我尝试了Google搜索,但没有成功.我正在使用g(lobal)标志.
And these are the two patterns I'm testing (?<=(\*[ \t]{3})).*
which works just as expected and matches the 2nd line, same if I replace 3 with 1, 2 or 4 however if I replace it with 1,4 forming the following pattern (?<=(\*[ \t]{1,4})).*
it no longer matches any of the rows and I honestly can't understand why. I've tried googling without success. I'm using the g(lobal) flag.
推荐答案
PHP与许多其他样式一样,不支持可变长度的后向查找.唯一的支持是后置条件顶层的 alternation (|
).甚至?
都可以破坏模式.一种替代方法是使用:
PHP, like many flavors, doesn't support variable length lookbehind. The only support is alternation (|
) at the top level of the lookbehind. Even a ?
can break the pattern. An alternative is to use:
(?<=\*[ \t]|\*[ \t]{2}|\*[ \t]{3}|\*[ \t]{4}).*
或者更好的方法是中止组的后退:
Or better, abort the lookbehind for a group:
\*[ \t]{1,4}(.*)
这应该对您来说很好,因为看起来您的比赛似乎并没有重叠.
This should work well for you, since it doesn't seem like you have overlapping of your matches anyway.
从手册中:
限制向后查找的断言的内容,以使它匹配的所有字符串都必须具有固定的长度.但是,如果有几种选择,则它们不必都具有相同的固定长度.因此,允许(?<== bullock | donkey),但是(?<!dogs?| cats?)在编译时会导致错误.匹配不同长度字符串的分支只允许在后向断言的顶层使用.
The contents of a lookbehind assertion are restricted such that all the strings it matches must have a fixed length. However, if there are several alternatives, they do not all have to have the same fixed length. Thus (?<=bullock|donkey) is permitted, but (?<!dogs?|cats?) causes an error at compile time. Branches that match different length strings are permitted only at the top level of a lookbehind assertion.
来源: http://www.php.net/manual/zh/regexp.reference.assertions.php
这篇关于量词范围在后面不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!