正则表达式:为什么负向前瞻后的数字捕获不起作用?

发布于 2025-01-14 03:11:44 字数 278 浏览 4 评论 0原文

(.)(?!\1)\1

我认为这应该匹配任何字符 [c],后跟任何不是 [c] 的字符,然后再次跟 [c]。

像“aba”、“xyx”

但在线正则表达式验证器告诉我我错了。问题出在哪里?

提前致谢。在此处输入图像描述

(.)(?!\1)\1

I think this should match any character [c], followed by any character that is not [c], then followed by [c] again.

like 'aba', 'xyx'

But online regexr validator is telling me im wrong. Where is the issue?

Thanks in advance.enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

卷耳 2025-01-21 03:11:44

您正确地检查了第二个字符是否与第一个(组)相同,但您忘记允许第二个字符匹配。

(.)(?!\1).\1

https://regex101 .com/r/vvapcB/1


如果您还希望匹配仅包含 3 个字符,则

(?=.{3}$)(.)(?!\1).\1

https://regex101.com/r/2IVxNK/1

You were correctly checking if the second character isn't the same as the first (group), but you forgot to allow a match on the second character otherwise.

(.)(?!\1).\1

https://regex101.com/r/vvapcB/1


If you want to also want the matches to be 3 characters only,

(?=.{3}$)(.)(?!\1).\1

https://regex101.com/r/2IVxNK/1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文