前瞻混乱
好吧,我从正则表达式食谱中得到了这个例子
^(?=.{3}$).*
上面的正则表达式用于限制任意模式的长度
如果我再次测试“aaabbb”,它完全失败
根据我的理解,它会查找前面有长度为 3 的任何字符的任何字符.SO它应该匹配“bbb”,但它不是
还有一个问题,应该lookbehind遵循这种模式x(?=x)
Ok i got this example from Regular Expression Cookbook
^(?=.{3}$).*
The regex above is use to limit the length of an arbitrary pattern
If i test again 'aaabbb', it completely fail
From what i understand it look for any character that precede by any character 3 in length.SO it should match 'bbb' but its not
One more question, should lookbehind follow this pattern x(?=x)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这实际上是一个前瞻断言而不是后视断言。 ^ 将匹配锚定在字符串的开头,然后它断言字符串的开头后面必须跟有 3 个字符,然后是字符串的结尾。
编辑:我应该提到最后的 .* 然后用于匹配这三个字符,因为先行断言不会消耗任何字符。
That is actually a lookahead assertion not a lookbehind assertion. The ^ anchors the match at the start of the string, it then asserts that the beginning of the string must be followed by 3 characters followed by the end of the string.
Edit: I should have probably mentioned that the .* at the end is then used to match those three characters since a lookahead assertion doesn't consume any characters.
!让我们仔细看看...
几个问题:
.*
是字符串中的第一个字符,然后您试图在它们后面查找夹在开始之间的字符^
和第一个字符.*
。.{3}
实际上表示任意三个字符,而不是任何重复三次的字符;)您实际上想知道如何使用 Perl 正则表达式查找重复的字母?Nope! Let's take a closer look...
Several problems:
.*
be the first characters in the string and then you're trying to lookbehind them for characters sandwhiched between the beginning^
and the first characters.*
..{3}
actually means any three characters, not any character repeated three times ;) You actually want to know How can I find repeated letters with a Perl regex?