前瞻混乱

发布于 2024-09-14 22:07:31 字数 221 浏览 3 评论 0原文

好吧,我从正则表达式食谱中得到了这个例子

^(?=.{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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与酒说心事 2024-09-21 22:07:31

这实际上是一个前瞻断言而不是后视断言。 ^ 将匹配锚定在字符串的开头,然后它断言字符串的开头后面必须跟有 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.

彩虹直至黑白 2024-09-21 22:07:31

据我了解,它会查找长度为 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

几个问题:

  1. 插入符号要求 .* 是字符串中的第一个字符,然后您试图在它们后面查找夹在开始 之间的字符^ 和第一个字符 .*
  2. 您的 .{3}​​ 实际上表示任意三个字符,而不是任何重复三次的字符;)您实际上想知道如何使用 Perl 正则表达式查找重复的字母?

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

Nope! Let's take a closer look...

^        # 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

Several problems:

  1. The caret requires that the .* 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 .*.
  2. Your .{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?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文