正则表达式:为什么负向前瞻后的数字捕获不起作用?
(.)(?!\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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正确地检查了第二个字符是否与第一个(组)相同,但您忘记允许第二个字符匹配。
(.)(?!\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