好的,我从正则表达手册获得了这个例子
^(?=.{3}$).*
上面的正则表达式用于限制任意模式的长度
如果我再次测试'aaabbb',则完全失败
根据我的理解,它寻找任何字符前面的任何字符3的长度。它应匹配'bbb'但不是
还有一个问题,应该遵循这种模式x(?= x)
答案 0 :(得分:6)
这实际上是一个前瞻性的断言,而不是一个外观断言。 ^在字符串的开头处锚定匹配,然后断言字符串的开头必须后跟3个字符,后跟字符串的结尾。
编辑:我应该提到最后的。*用于匹配这三个字符,因为前瞻断言不会消耗任何字符。
答案 1 :(得分:4)
根据我的理解,它寻找任何字符前面的任何字符3的长度。它应匹配'bbb'但不是
都能跟得上!让我们仔细看看......
^ # The caret is an anchor which denotes "STARTS WITH"
(?= # lookahead
. # wildcard match; the . matches any non-new-line character
{3} # quantifier; exactly 3 times
$ # dollar sign; I'm not sure if it will act as an anchor but if it did it would mean "THE END"
) # end of lookbehind
. # wildcard match; the . matches any non-new-line character
* # quantifier; any number of times, including 0 times
几个问题:
.*
是字符串中的第一个字符,然后您试图在它们后面查看开头^
和第一个字符.*
之间的字符。 .{3}
实际上意味着任意三个字符,而不是任何重复三次的字符;)您实际上想知道How can I find repeated letters with a Perl regex?