正则表达式否定? [英] Regex negation?

查看:38
本文介绍了正则表达式否定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在打正则表达式高尔夫 (http://regex.alf.nu/),我正在打阿巴洞.我有以下正则表达式完全匹配错误的一面(这是我试图做的):

I'm playing Regex Golf (http://regex.alf.nu/) and I'm doing the Abba hole. I have the following regex that matches the wrong side entirely (which is what I was trying to do):

(([\w])([\w])\3\2)

但是,我现在试图否定它,以便它与另一边匹配.我似乎无法弄清楚那部分.我试过了:

However, I'm trying to negate it now so it matches the other side. I can't seem to figure that part out. I tried:

(?!([\w])([\w])\3\2)

但这没有用.来自正则表达式大师的任何提示?

But that didn't work. Any tips from the regex masters?

推荐答案

您可以通过简单地使用 . 并删除不必要的括号来缩短它(并获得更多积分):

You can make it much shorter (and get more points) by simply using . and removing unnecessary parens:

^(?!.*(.)(.)\2\1)

它只是确保字符串的任何部分都没有abba"(这里的abba"表示我们不想匹配的特定顺序中的 4 个字母),而不必匹配整个单词.

It just makes sure that there's no "abba" ("abba" here means 4 letters in that particular order we don't want to match) in any part of the string without having to match the whole word.

这篇关于正则表达式否定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆