正则表达式匹配 3 个或更多连续连续字符和连续相同字符

发布于 2024-12-27 14:47:37 字数 128 浏览 3 评论 0原文

我需要正则表达式来匹配以下情况。

  1. 3 个或更多连续的连续字符/数字;例如123、abc、789、pqr等
  2. 3个或更多连续相同的字符/数字;例如 111、aaa、bbb、222 等。

I need regular expressions to match the below cases.

  1. 3 or more consecutive sequential characters/numbers; e.g. 123, abc, 789, pqr, etc.
  2. 3 or more consecutive identical characters/numbers; e.g. 111, aaa, bbb, 222, etc.

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

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

发布评论

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

评论(15

因为看清所以看轻 2025-01-03 14:47:37

我认为您不能(轻松)在第一种情况下使用正则表达式。第二种情况很简单:

Pattern pattern = Pattern.compile("([a-z\\d])\\1\\1", Pattern.CASE_INSENSITIVE);

由于 \\1 表示与组 1 匹配的部分,这将匹配位于 az 范围内的三个相同字符的任何序列 或数字 (\d)。

更新

需要明确的是,对于第一种情况,您可以使用正则表达式。然而,这种模式是如此费力且极其复杂,所以你最好根本不这样做。特别是如果你想真正涵盖所有字母表。在这种情况下,您可能应该通过迭代 Unicode 字符集的字符代码或类似的东西以编程方式生成模式,并为每三个连续字符生成分组。但是,您应该意识到,通过为模式匹配器生成如此大的决策树,行进性能必然会受到影响(O(n),其中 n 是数字组的大小是 Unicode 字符集的大小减 2)。

I don't think you can (easily) use regex for the first case. The second case is easy though:

Pattern pattern = Pattern.compile("([a-z\\d])\\1\\1", Pattern.CASE_INSENSITIVE);

Since \\1 represents part matched by group 1 this will match any sequence of three identical characters that are either within the range a-z or are digits (\d).

Update

To be clear, you can use regex for the first case. However, the pattern is so laborious and ridiculously convoluted that you are better off not doing it at all. Especially if you wanted to REALLY cover all the alphabet. In that case you should probably generate the pattern programmatically by iterating the char codes of the Unicode charset or something like that and generate groupings for every three consecutive characters. However, you should realize that by having generated such a large decision tree for the pattern matcher, the marching performance is bound to suffer (O(n) where n is the number of groups which is the size of the Unicode charset minus 2).

苍暮颜 2025-01-03 14:47:37

我不同意,情况 1 可以用于正则表达式,但你必须告诉它要匹配的序列...这有点长而无聊:

/(abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz|012|123|234|345|456|567|678|789)+/ig

http://regexr.com/3dqln

I disagree, case 1 is possible to regex, but you have to tell it the sequences to match... which is kind of long and boring:

/(abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz|012|123|234|345|456|567|678|789)+/ig

http://regexr.com/3dqln

苦行僧 2025-01-03 14:47:37

对于第二个问题:

\\b([a-zA-Z0-9])\\1\\1+\\b

解释:

\\b               : zero-length word boundary
  (               : start capture group 1
    [a-zA-Z0-9]   : a letter or a digit
  )               : end group
  \\1             : same character as group 1
  \\1+            : same character as group 1 one or more times
\\b               : zero-length word boundary

for the second question:

\\b([a-zA-Z0-9])\\1\\1+\\b

explanation:

\\b               : zero-length word boundary
  (               : start capture group 1
    [a-zA-Z0-9]   : a letter or a digit
  )               : end group
  \\1             : same character as group 1
  \\1+            : same character as group 1 one or more times
\\b               : zero-length word boundary
七颜 2025-01-03 14:47:37

据我所知,第一种情况确实不可能。正则表达式引擎不知道任何关于自然数或字母表的顺序。但至少可以区分 3 个或更多数字和 3 个或更多字母,例如:

[a-z]{3,}|[A-Z]{3,}|\d{3,}

这匹配 abcdABCDE123 但例如,与 ab2dA5c412z 不匹配。据此,第二种情况可以用较短的版本正确给出:

  (\w)\1{2,}

To my knowledge, the first case is indeed not possible. The regex engine doesn't know anything about the order of the natural numbers or the alphabet. But it's at least possible to differentiate between 3 or more numbers and 3 or more letters, for example:

[a-z]{3,}|[A-Z]{3,}|\d{3,}

This matches abcd, ABCDE or 123 but doesn't match ab2d, A5c4 or 12z, for example. According to this, the second case can be correctly given in a shorter version as:

  (\w)\1{2,}
2025-01-03 14:47:37

3 个或更多连续的连续字符/数字,例如 - 123、abc、789、pqr 等。

正则表达式不可能。

3 个或更多连续的相同字符/数字,例如 - 111、aaa、bbb。 222等

使用模式 (?i)(?:([a-z0-9])\\1{2,})*

如果要检查整个字符串,请使用 Matcher.matches()。要查找字符串中的匹配项,请使用 Matcher.find()

这是一些示例代码:

final String ps = "(?i)(?:([a-z0-9])\\1{2,})*";
final String psLong =
        "(?i)\t\t\t# Case insensitive flag\n"
                + "(?:\t\t\t\t# Begin non-capturing group\n"
                + " (\t\t\t\t# Begin capturing group\n"
                + "  [a-z0-9]\t\t# Match an alpha or digit character\n"
                + " )\t\t\t\t# End capturing group\n"
                + " \\1\t\t\t\t# Back-reference first capturing group\n"
                + " {2,}\t\t\t# Match previous atom 2 or more times\n"
                + ")\t\t\t\t# End non-capturing group\n"
                + "*\t\t\t\t# Match previous atom zero or more characters\n";
System.out.println("***** PATTERN *****\n" + ps + "\n" + psLong
        + "\n");
final Pattern p = Pattern.compile(ps);
for (final String s : new String[] {"aa", "11", "aaa", "111",
        "aaaaaaaaa", "111111111", "aaa111bbb222ccc333",
        "aaaaaa111111bbb222"})
{
    final Matcher m = p.matcher(s);
    if (m.matches()) {
        System.out.println("Success: " + s);
    } else {
        System.out.println("Fail: " + s);
    }
}

输出是:

***** PATTERN *****
(?i)(?:([a-z0-9])\1{2,})*
(?i)            # Case insensitive flag
(?:             # Begin non-capturing group
 (              # Begin capturing group
  [a-z0-9]      # Match an alpha or digit character
 )              # End capturing group
 \1             # Back-reference first capturing group
 {2,}           # Match previous atom 2 or more times
)               # End non-capturing group
*               # Match previous atom zero or more characters


Fail: aa
Fail: 11
Success: aaa
Success: 111
Success: aaaaaaaaa
Success: 111111111
Success: aaa111bbb222ccc333
Success: aaaaaa111111bbb222

3 or more consecutive sequential characters/numbers ex - 123, abc, 789, pqr etc.

Not possible with regular expressions.

3 or more consecutive identical characters/numbers ex - 111, aaa, bbb. 222 etc.

Use a pattern of (?i)(?:([a-z0-9])\\1{2,})*.

If you want to check the whole string, use Matcher.matches(). To find matches within a string, use Matcher.find().

Here's some sample code:

final String ps = "(?i)(?:([a-z0-9])\\1{2,})*";
final String psLong =
        "(?i)\t\t\t# Case insensitive flag\n"
                + "(?:\t\t\t\t# Begin non-capturing group\n"
                + " (\t\t\t\t# Begin capturing group\n"
                + "  [a-z0-9]\t\t# Match an alpha or digit character\n"
                + " )\t\t\t\t# End capturing group\n"
                + " \\1\t\t\t\t# Back-reference first capturing group\n"
                + " {2,}\t\t\t# Match previous atom 2 or more times\n"
                + ")\t\t\t\t# End non-capturing group\n"
                + "*\t\t\t\t# Match previous atom zero or more characters\n";
System.out.println("***** PATTERN *****\n" + ps + "\n" + psLong
        + "\n");
final Pattern p = Pattern.compile(ps);
for (final String s : new String[] {"aa", "11", "aaa", "111",
        "aaaaaaaaa", "111111111", "aaa111bbb222ccc333",
        "aaaaaa111111bbb222"})
{
    final Matcher m = p.matcher(s);
    if (m.matches()) {
        System.out.println("Success: " + s);
    } else {
        System.out.println("Fail: " + s);
    }
}

And the output is:

***** PATTERN *****
(?i)(?:([a-z0-9])\1{2,})*
(?i)            # Case insensitive flag
(?:             # Begin non-capturing group
 (              # Begin capturing group
  [a-z0-9]      # Match an alpha or digit character
 )              # End capturing group
 \1             # Back-reference first capturing group
 {2,}           # Match previous atom 2 or more times
)               # End non-capturing group
*               # Match previous atom zero or more characters


Fail: aa
Fail: 11
Success: aaa
Success: 111
Success: aaaaaaaaa
Success: 111111111
Success: aaa111bbb222ccc333
Success: aaaaaa111111bbb222
嗳卜坏 2025-01-03 14:47:37

感谢大家对我的帮助。

对于第一种情况 - 3 个或更多连续的连续字符/数字;例如123、abc、789、pqr等。我使用了下面的代码逻辑。请分享您对此的评论。

public static boolean validateConsecutiveSeq(String epin) {
    char epinCharArray[] = epin.toCharArray();
    int asciiCode = 0;
    boolean isConSeq = false;
    int previousAsciiCode = 0;
    int numSeqcount = 0;

    for (int i = 0; i < epinCharArray.length; i++) {
        asciiCode = epinCharArray[i];
        if ((previousAsciiCode + 1) == asciiCode) {
            numSeqcount++;
            if (numSeqcount >= 2) {
                isConSeq = true;
                break;
            }
        } else {
            numSeqcount = 0;
        }
        previousAsciiCode = asciiCode;
    }
    return isConSeq;
}

Thanks All for helping me.

For the first case - 3 or more consecutive sequential characters/numbers; e.g. 123, abc, 789, pqr, etc. I used below code logic. Pls share your comments on this.

public static boolean validateConsecutiveSeq(String epin) {
    char epinCharArray[] = epin.toCharArray();
    int asciiCode = 0;
    boolean isConSeq = false;
    int previousAsciiCode = 0;
    int numSeqcount = 0;

    for (int i = 0; i < epinCharArray.length; i++) {
        asciiCode = epinCharArray[i];
        if ((previousAsciiCode + 1) == asciiCode) {
            numSeqcount++;
            if (numSeqcount >= 2) {
                isConSeq = true;
                break;
            }
        } else {
            numSeqcount = 0;
        }
        previousAsciiCode = asciiCode;
    }
    return isConSeq;
}
笑梦风尘 2025-01-03 14:47:37

匹配三个连续数字或字母的正则表达式是
([0-9]|[aA-zZ])\1\1

Regex to match three consecutive numbers or alphabets is
"([0-9]|[aA-zZ])\1\1"

忘羡 2025-01-03 14:47:37

如果您有下限 (3) 和上限 regexString 可以生成如下

public class RegexBuilder {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();

        int seqStart = 3;
        int seqEnd = 5;
        buildRegex(sb, seqStart, seqEnd);
        System.out.println(sb);
    }

    private static void buildRegex(StringBuilder sb, int seqStart, int seqEnd) {
        for (int i = seqStart; i <= seqEnd; i++) {
            buildRegexCharGroup(sb, i, '0', '9');
            buildRegexCharGroup(sb, i, 'A', 'Z');
            buildRegexCharGroup(sb, i, 'a', 'z');
            buildRegexRepeatedString(sb, i);
        }
    }

    private static void buildRegexCharGroup(StringBuilder sb, int seqLength,
            char start, char end) {
        for (char c = start; c <= end - seqLength + 1; c++) {
            char ch = c;
            if (sb.length() > 0) {
                sb.append('|');
            }
            for (int i = 0; i < seqLength; i++) {
                sb.append(ch++);
            }
        }
    }

    private static void buildRegexRepeatedString(StringBuilder sb, int seqLength) {
        sb.append('|');
        sb.append("([a-zA-Z\\d])");
        for (int i = 1; i < seqLength; i++) {
            sb.append("\\1");
        }
    }
}

输出

012|123|234|345|456|567|678|789|ABC|BCD|CDE|DEF|EFG|FGH|GHI|HIJ|IJK|JKL|KLM|LMN|MNO|NOP|OPQ|PQR|QRS|RST|STU|TUV|UVW|VWX|WXY|XYZ|abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz|([a-z\d])\1\1|0123|1234|2345|3456|4567|5678|6789|ABCD|BCDE|CDEF|DEFG|EFGH|FGHI|GHIJ|HIJK|IJKL|JKLM|KLMN|LMNO|MNOP|NOPQ|OPQR|PQRS|QRST|RSTU|STUV|TUVW|UVWX|VWXY|WXYZ|abcd|bcde|cdef|defg|efgh|fghi|ghij|hijk|ijkl|jklm|klmn|lmno|mnop|nopq|opqr|pqrs|qrst|rstu|stuv|tuvw|uvwx|vwxy|wxyz|([a-z\d])\1\1\1|01234|12345|23456|34567|45678|56789|ABCDE|BCDEF|CDEFG|DEFGH|EFGHI|FGHIJ|GHIJK|HIJKL|IJKLM|JKLMN|KLMNO|LMNOP|MNOPQ|NOPQR|OPQRS|PQRST|QRSTU|RSTUV|STUVW|TUVWX|UVWXY|VWXYZ|abcde|bcdef|cdefg|defgh|efghi|fghij|ghijk|hijkl|ijklm|jklmn|klmno|lmnop|mnopq|nopqr|opqrs|pqrst|qrstu|rstuv|stuvw|tuvwx|uvwxy|vwxyz|([a-z\d])\1\1\1\1

If you have lower bound (3) and upper bound regexString can be generated as follows

public class RegexBuilder {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();

        int seqStart = 3;
        int seqEnd = 5;
        buildRegex(sb, seqStart, seqEnd);
        System.out.println(sb);
    }

    private static void buildRegex(StringBuilder sb, int seqStart, int seqEnd) {
        for (int i = seqStart; i <= seqEnd; i++) {
            buildRegexCharGroup(sb, i, '0', '9');
            buildRegexCharGroup(sb, i, 'A', 'Z');
            buildRegexCharGroup(sb, i, 'a', 'z');
            buildRegexRepeatedString(sb, i);
        }
    }

    private static void buildRegexCharGroup(StringBuilder sb, int seqLength,
            char start, char end) {
        for (char c = start; c <= end - seqLength + 1; c++) {
            char ch = c;
            if (sb.length() > 0) {
                sb.append('|');
            }
            for (int i = 0; i < seqLength; i++) {
                sb.append(ch++);
            }
        }
    }

    private static void buildRegexRepeatedString(StringBuilder sb, int seqLength) {
        sb.append('|');
        sb.append("([a-zA-Z\\d])");
        for (int i = 1; i < seqLength; i++) {
            sb.append("\\1");
        }
    }
}

Output

012|123|234|345|456|567|678|789|ABC|BCD|CDE|DEF|EFG|FGH|GHI|HIJ|IJK|JKL|KLM|LMN|MNO|NOP|OPQ|PQR|QRS|RST|STU|TUV|UVW|VWX|WXY|XYZ|abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz|([a-z\d])\1\1|0123|1234|2345|3456|4567|5678|6789|ABCD|BCDE|CDEF|DEFG|EFGH|FGHI|GHIJ|HIJK|IJKL|JKLM|KLMN|LMNO|MNOP|NOPQ|OPQR|PQRS|QRST|RSTU|STUV|TUVW|UVWX|VWXY|WXYZ|abcd|bcde|cdef|defg|efgh|fghi|ghij|hijk|ijkl|jklm|klmn|lmno|mnop|nopq|opqr|pqrs|qrst|rstu|stuv|tuvw|uvwx|vwxy|wxyz|([a-z\d])\1\1\1|01234|12345|23456|34567|45678|56789|ABCDE|BCDEF|CDEFG|DEFGH|EFGHI|FGHIJ|GHIJK|HIJKL|IJKLM|JKLMN|KLMNO|LMNOP|MNOPQ|NOPQR|OPQRS|PQRST|QRSTU|RSTUV|STUVW|TUVWX|UVWXY|VWXYZ|abcde|bcdef|cdefg|defgh|efghi|fghij|ghijk|hijkl|ijklm|jklmn|klmno|lmnop|mnopq|nopqr|opqrs|pqrst|qrstu|rstuv|stuvw|tuvwx|uvwxy|vwxyz|([a-z\d])\1\1\1\1
网名女生简单气质 2025-01-03 14:47:37

全部放在一起:

([a-zA-Z0-9])\1\1+|(abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop |opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz|012|123|234|345|456|567|678|789)+

3 个或更多连续的连续字符/数字;例如 123、abc、789、pqr 等。

(abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy| xyz|012|123|234|345|456|567|678|789)+

3个或更多连续相同字符/数字;例如 111、aaa、bbb、222 等。

([a-zA-Z0-9])\1\1+

https://regexr.com/4727n

这也有效:

(?:(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6) |6(?=7)|7(?=8)|8(?=9)){2 ,}\d|(?:a(?=b)|b(?=c)|c(?=d)|d(?=e)|e(?=f)|f(?=g)| g(?=h)|h(?=i)|i(?=j)|j(? =k)​​|k(?=l)|l(?=m)|m(?=n)|n(?=o)|o(?=p)|p(?=q)|q(?= r)|r(?=s)|s(?=t)|t(?=u)| u(?=v)|v(?=w)|w(?=x)|x(?=y)|y(?=z)){2,}[[:alpha:]])|([ a-zA-Z0-9])\1\1+

https://regex101.com/r/6fXC9u/1

All put together:

([a-zA-Z0-9])\1\1+|(abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz|012|123|234|345|456|567|678|789)+

3 or more consecutive sequential characters/numbers; e.g. 123, abc, 789, pqr, etc.

(abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz|012|123|234|345|456|567|678|789)+

3 or more consecutive identical characters/numbers; e.g. 111, aaa, bbb, 222, etc.

([a-zA-Z0-9])\1\1+

https://regexr.com/4727n

This also works:

(?:(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){2,}\d|(?:a(?=b)|b(?=c)|c(?=d)|d(?=e)|e(?=f)|f(?=g)|g(?=h)|h(?=i)|i(?=j)|j(?=k)|k(?=l)|l(?=m)|m(?=n)|n(?=o)|o(?=p)|p(?=q)|q(?=r)|r(?=s)|s(?=t)|t(?=u)|u(?=v)|v(?=w)|w(?=x)|x(?=y)|y(?=z)){2,}[[:alpha:]])|([a-zA-Z0-9])\1\1+

https://regex101.com/r/6fXC9u/1

还给你自由 2025-01-03 14:47:37

对于第一个问题,如果您可以使用较少的正则表达式,则此方法有效

         containsConsecutiveCharacters(str) {
            for (let i = 0; i <= str.length - 3; i++) {
                var allthree = str[i] + str[i + 1] + str[i + 2];
                let s1 = str.charCodeAt(i);
                let s2 = str.charCodeAt(i + 1);
                let s3 = str.charCodeAt(i + 2);
                if (
                    /[a-zA-Z]+$/.test(allthree) &&
                    (s1 < s2 && s2 < s3 && s1+s2+s3-(3*s1) === 3)
                ) {
                    return true;
                }
            }
        }

for the first question this works if you're ok with less regex

         containsConsecutiveCharacters(str) {
            for (let i = 0; i <= str.length - 3; i++) {
                var allthree = str[i] + str[i + 1] + str[i + 2];
                let s1 = str.charCodeAt(i);
                let s2 = str.charCodeAt(i + 1);
                let s3 = str.charCodeAt(i + 2);
                if (
                    /[a-zA-Z]+$/.test(allthree) &&
                    (s1 < s2 && s2 < s3 && s1+s2+s3-(3*s1) === 3)
                ) {
                    return true;
                }
            }
        }
  • <块引用>

    3个或更多连续的连续字符/数字;例如123、abc、789、pqr等

    <预><代码>(?:(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(? =6)|6(?=7)|7(?= 8)|8(?=9)){2,}\d|(?:a(?=b)|b(?=c)|c(?=d)|d(?=e)|e( ?=f)|f(?=g)|g(?=h)|h (?=i)|i(?=j)|j(?=k)|k(?=l)|l(?=m)|m(?=n)|n(?=o)|o( ?=p)|p(?=q)|q(?=r)|r (?=s)|s(?=t)|t(?=u)|u(?=v)|v(?=w)|w(?=x)|x(?=y)|y( ?=z)){2,}[\p{Alpha}])

    https://regex101.com/r/5IragF/1

  • <块引用>

    3个或更多连续的相同字符/数字;例如 111、aaa、bbb、222 等

    <前><代码>([\p{Alnum}])\1{2,}

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

  • 3 or more consecutive sequential characters/numbers; e.g. 123, abc, 789, pqr, etc.

    (?:(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){2,}\d|(?:a(?=b)|b(?=c)|c(?=d)|d(?=e)|e(?=f)|f(?=g)|g(?=h)|h(?=i)|i(?=j)|j(?=k)|k(?=l)|l(?=m)|m(?=n)|n(?=o)|o(?=p)|p(?=q)|q(?=r)|r(?=s)|s(?=t)|t(?=u)|u(?=v)|v(?=w)|w(?=x)|x(?=y)|y(?=z)){2,}[\p{Alpha}])
    

    https://regex101.com/r/5IragF/1

  • 3 or more consecutive identical characters/numbers; e.g. 111, aaa, bbb, 222, etc.

    ([\p{Alnum}])\1{2,}
    

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

如此安好 2025-01-03 14:47:37

全部放在一起:

([a-zA-Z0-9])\1\1+|(abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop |opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz|012|123|234|345|456|567|678|789)+

3 个或更多连续的连续字符/数字;例如 123、abc、789、pqr 等。

(abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy| xyz|012|123|234|345|456|567|678|789)+

3个或更多连续相同字符/数字;例如 111、aaa、bbb、222 等。

([a-zA-Z0-9])\1\1+

https://regexr.com/4727n

All put together:

([a-zA-Z0-9])\1\1+|(abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz|012|123|234|345|456|567|678|789)+

3 or more consecutive sequential characters/numbers; e.g. 123, abc, 789, pqr, etc.

(abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz|012|123|234|345|456|567|678|789)+

3 or more consecutive identical characters/numbers; e.g. 111, aaa, bbb, 222, etc.

([a-zA-Z0-9])\1\1+

https://regexr.com/4727n

甜尕妞 2025-01-03 14:47:37

对于案例 #2,我受到 regextester 上的示例的启发,并创建了以下正则表达式来匹配 n 个相同的数字(要检查数字和字母,请将 0-9 替换为 A-Za-z0-9):

const n = 3
const identicalAlphanumericRegEx = new RegExp("([0-9])" + "\\1".repeat(n - 1))

For case #2 I got inspired by a sample on regextester and created the following regex to match n identical digits (to check for both numbers and letters replace 0-9 with A-Za-z0-9):

const n = 3
const identicalAlphanumericRegEx = new RegExp("([0-9])" + "\\1".repeat(n - 1))
动听の歌 2025-01-03 14:47:37

我正在与一位同事讨论这个问题,我们认为我们对#1 有一个很好的解决方案。

要检查 abcbcd 或 ... 或 012123 甚至任意数量的连续字符,尝试:

.*((a(?=b))|(?:b(?=c))|(?:c(?=d))|(?:d(?=e))|(?:e(?=f))|(?:f(?=g))|(?:g(?=h))|(?:h(?=i))|(?:i(?=j))|(?:j(?=k))|(?:k(?=l))|(?:l(?=m))|(?:m(?=n))|(?:n(?=o))|(?:o(?=p))|(?:p(?=q))|(?:q(?=r))|(?:r(?=s))|(?:s(?=t))|(?:t(?=u))|(?:u(?=v))|(?:v(?=w))|(?:w(?=x))|(?:x(?=y))|(?:y(?=z))|(?:0(?=1))|(?:1(?=2))|(?:2(?=3))|(?:3(?=4))|(?:4(?=5))|(?:5(?=6))|(?:6(?=7))|(?:7(?=8))|(?:8(?=9))){2,}.*

此解决方案的好处是,如果您想要超过 3 个连续字符,请将 {2,} 增加到比您要检查的值少 1 的值。

每个组中的 ?: 可防止该组被捕获。

I was discussing this with a coworker and we think we have a good solution for #1.

To check for abc or bcd or ... or 012 or 123 or even any number of sequential characters, try:

.*((a(?=b))|(?:b(?=c))|(?:c(?=d))|(?:d(?=e))|(?:e(?=f))|(?:f(?=g))|(?:g(?=h))|(?:h(?=i))|(?:i(?=j))|(?:j(?=k))|(?:k(?=l))|(?:l(?=m))|(?:m(?=n))|(?:n(?=o))|(?:o(?=p))|(?:p(?=q))|(?:q(?=r))|(?:r(?=s))|(?:s(?=t))|(?:t(?=u))|(?:u(?=v))|(?:v(?=w))|(?:w(?=x))|(?:x(?=y))|(?:y(?=z))|(?:0(?=1))|(?:1(?=2))|(?:2(?=3))|(?:3(?=4))|(?:4(?=5))|(?:5(?=6))|(?:6(?=7))|(?:7(?=8))|(?:8(?=9))){2,}.*

The nice thing about this solution is if you want more than 3 consecutive characters, increase the {2,} to be one less than what you want to check for.

the ?: in each group prevents the group from being captured.

泪之魂 2025-01-03 14:47:37

尝试这个来回答第一个问题。

如果在 arg 中找到 3 个连续的数字或字母,则返回 true

function check(val){
    for (i = 0; i <= val.length - 3; i++) {
      var s1 = val.charCodeAt(i);
      var s2 = val.charCodeAt(i + 1);
      var s3 = val.charCodeAt(i + 2);
      if (Math.abs(s1 - s2) === 1 && s1 - s2 === s2 - s3) {
        return true;
      }
    }

    return false;
}

console.log(check('Sh1ak@ki1r@100'));

Try this for the first question.

returns true if it finds 3 consecutive numbers or alphabets in the arg

function check(val){
    for (i = 0; i <= val.length - 3; i++) {
      var s1 = val.charCodeAt(i);
      var s2 = val.charCodeAt(i + 1);
      var s3 = val.charCodeAt(i + 2);
      if (Math.abs(s1 - s2) === 1 && s1 - s2 === s2 - s3) {
        return true;
      }
    }

    return false;
}

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