简体   繁体   English

正则表达式不包括子模式

[英]Regular expression excluding subpatterns

I have got codes to match with a very simple string pattern: XXnnnnnnnnn (2 alphanumeric, 9 numeric) . 我有代码与一个非常简单的字符串模式匹配: XXnnnnnnnnn (2 alphanumeric, 9 numeric)

I am using this regex: \\w{2}\\d{9} . 我正在使用这个正则表达式: \\w{2}\\d{9}

Now I am required to excude from matching any string that begins with the constant token ' AY ', and any string with 11 repeated character (eg ' 11111111111 ' or ' 00000000000 '). 现在我需要通过匹配以常量标记' AY '开头的任何字符串以及任何具有11个重复字符的字符串(例如' 11111111111 '或' 00000000000 ')来排除。

How may I exlude subpatterns using regular expressions? 我怎样才能使用正则表达式排除子模式?

Try this 试试这个

\b(?!AY)(?!(\w)\1{10})\w{2}\d{9}\b

See it here on Regexr 在Regexr上看到它

Basically I added only the \\b word boundaries to your regex to avoid partial matches. 基本上我只在你的正则表达式中添加\\b 字边界以避免部分匹配。

Your restrictions are achieved by using negative lookahead assertions. 通过使用负前瞻断言来实现您的限制。

(?!AY) The assertion fails if the pattern starts with "AY" (?!AY)如果模式以“AY”开头,则断言失败

(?!(\\w)\\1{10}) The assertion fails if the first word character is repeated 10 more times. (?!(\\w)\\1{10})如果第一个单词字符重复10次,则断言失败。

Lookaround assertions on regular-expressions.info 在regular-expressions.info上查看断言

You can use zero-width look-ahead matchers to exclude those two cases. 您可以使用零宽度前瞻匹配器来排除这两种情况。

Not matching AY is easy: 不匹配AY很容易:

 (?!AY)

Not matching 11 of the same characters is more difficult. 不匹配11个相同的字符更难。 You either need to use back references (which means the regexp isn't strictly regular anymore), or you can match explicitly to each digit. 您需要使用后引用(这意味着正则表达式不再是严格规则),或者您可以明确匹配每个数字。

(?!0{11}|1{11}|2{11}|…|9{11})  // 11 zeros, or 11 ones, or 11 twos, …
(?!(.)\1{10})  // A character, followed by itself 10 more times.

So that would make the full regexp: 所以这将使完整的正则表达式:

(?!AY)(?!(.)\1{10})\w{2}\d{9}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM