简体   繁体   English

不超过两个重复字母/数字的正则表达式

[英]Regular expression for no more than two repeated letters/digits

I have a requirement to handle a regular expression for no more than two of the same letters/digits in an XSL file.我需要处理 XSL 文件中不超过两个相同字母/数字的正则表达式。

  • no space没有空间
  • does not support special chars不支持特殊字符
  • support (az,AZ,0-9)支持 (az,AZ,0-9)
  • require one of az需要az之一
  • require one of 0-9需要 0-9 之一
  • no more than 2 same letter/digits (ie, BBB will fail, BB is accepted)不超过 2 个相同的字母/数字(即BBB将失败, BB被接受)

What I have so far到目前为止我所拥有的

(?:[^a-zA-Z0-9]{1,2})

This regex will do it: ^(?!.*([A-Za-z0-9])\\1{2})(?=.*[az])(?=.*\\d)[A-Za-z0-9]+$这个正则表达式会这样做: ^(?!.*([A-Za-z0-9])\\1{2})(?=.*[az])(?=.*\\d)[A-Za-z0-9]+$

Here's the breakdown:这是细分:

(?!.*([A-Za-z0-9])\\1{2}) makes sure that none of the chars repeat more than twice in a row. (?!.*([A-Za-z0-9])\\1{2})确保没有一个字符连续重复两次以上。

(?=.*[az]) requires at least one lowercase letter (?=.*[az])至少需要一个小写字母

(?=.*\\d) requires at least one digit (?=.*\\d)至少需要一位

[A-Za-z0-9]+ allows only letters and digits [A-Za-z0-9]+只允许字母和数字

EDIT : removed an extraneous .* from the negative lookahead编辑:从负前瞻中删除了一个无关紧要的.*

For matching the same character repeated 3 or more times consecutively, try:要匹配连续重复 3 次或更多次的相同字符,请尝试:

([a-zA-Z0-9])\1{2,}

Sample matches (tested both here and here ): AABBAA (no matches), AABBBAAA (matches BBB and AAA ), ABABABABABABABA (no matches), ABCCCCCCCCCC (matches CCCCCCCCCC ).样品匹配(测试都在这里这里): AABBAA (不匹配), AABBBAAA (匹配BBBAAA ), ABABABABABABABA (不匹配), ABCCCCCCCCCC (比赛CCCCCCCCCC )。

Does this one work for you?这个对你有用吗?

/(\b(?:([A-Za-z0-9])(?!\2{2}))+\b)/

Try it out:试试看:

var regex = new RegExp(/(\b(?:([A-Za-z0-9])(?!\2{2}))+\b)/)
var tests = ['A1D3E', 'AAAA', 'AABAA', 'abccddeeff', 'abbbc', '1234']

for(test in tests) {
   console.log(tests[test] + ' - ' + Boolean(tests[test].match(regex)))
}

Will output:将输出:

A1D3E - true
AAAA - false
AABAA - true
abccddeeff - true
abbbc - false
1234 - true

You may do this in 2 regexes:您可以在 2 个正则表达式中执行此操作:

  1. /^(?=.*[az])(?=.*[0-9])[a-z0-9]+$/i This will assure that there is at least 1 digit and 1 letter while accepting only letters and digits (no space or special characters) /^(?=.*[az])(?=.*[0-9])[a-z0-9]+$/i这将确保至少有 1 个数字和 1 个字母,同时只接受字母和数字(没有空格或特殊字符)
  2. /([a-z0-9])\\1{2,}/i If this one is matched, then there is a repeated character. /([a-z0-9])\\1{2,}/i如果匹配到这个,那么就有重复的字符了。 Which means you should throw false .这意味着你应该抛出false

Explanation:解释:

First regex:第一个正则表达式:

  • ^ : match begin of line ^ : 匹配行首
  • (?=.*[az]) : check if there is at least one letter (?=.*[az]) : 检查是否至少有一个字母
  • (?=.*[0-9]) : check if there is at least one digit (?=.*[0-9]) : 检查是否至少有一位
  • [a-z0-9]+ : if the checks were true, then match only digits/letters one or more times [a-z0-9]+ :如果检查为真,则只匹配数字/字母一次或多次
  • $ : match end of line $ : 匹配行尾
  • i : modifier, match case insensitive i : 修饰符,匹配不区分大小写

Second regex:第二个正则表达式:

  • ([a-z0-9]) : match and group a digit or a letter ([a-z0-9]) : 匹配和分组一个数字或一个字母
  • \\1{2,} : match group 1 two or more times \\1{2,} : 匹配组 1 两次或更多次
  • i : modifier, match case insensitive i : 修饰符,匹配不区分大小写

In response to a clarification, it seems that a single regular expression isn't strictly required.作为对澄清的回应,似乎并不严格要求单个正则表达式。 In that case I suggest you use several regular expressions or functions.在这种情况下,我建议您使用几个正则表达式或函数。 My guess is, performance isn't a requirement, since usually these sorts of checks are done in response to user input.我的猜测是,性能不是必需的,因为通常这些类型的检查是为了响应用户输入而完成的。 User input validation can take 100ms and still appear to be instant, and you can run a lot of code in 100ms.用户输入验证可能需要 100 毫秒,而且看起来仍然是即时的,并且您可以在 100 毫秒内运行大量代码。

For example, I personally would do a check for each of your conditions in a separate test.例如,我个人会在单独的测试中检查您的每个条件。 First, check for spaces.首先,检查空格。 Second, check for at least one letter.其次,检查至少一封信。 Next, check for at least one number.接下来,检查至少一个数字。 Finally, look for any spans of three or more repeated characters.最后,查找三个或更多重复字符的跨度。

Your code will be much easier to understand, and it will be much easier to modify the rules later (which, experience has shown, is almost certainly going to happen).您的代码将更容易理解,并且以后修改规则也更容易(经验表明,这几乎肯定会发生)。

For example:例如:

function do_validation(string) {
    return (has_no_space(string) &&
            has_no_special_char(string) &&
            has_alpha(string) &&
            has_digit(string) &&
            ! (has_repeating(string)))

I personally consider the above to be orders of magnitude easier to read than one complex regular expression.我个人认为上述内容比一个复杂的正则表达式更容易阅读。 Plus, adding or removing a rule doesn't make you have to reimplement a complex regular expression (and thus, be required to re-test all possible combinations).另外,添加或删除规则不会使您必须重新实现复杂的正则表达式(因此,需要重新测试所有可能的组合)。

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

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