简体   繁体   English

正则表达式匹配仅重复2位数字的数字

[英]Regular Expression Matching for number only with 2 digits repeated

I am trying to match number which are having 2 digits and those are repeated and the length of number is 7 digits . 我正在尝试匹配具有2位数字并且重复的数字,数字的长度为7位数字。

I want to match these numbers from java . 我想从java匹配这些数字。

example numbers: 示例编号:

3433434
6776767
9000999

Please help to create the regular expression for these pattern numbers 请帮助为这些模式编号创建正则表达式

I'd recommend hiding any regexes inside helper methods: 我建议在帮助器方法中隐藏所有正则表达式:

private static boolean matchesCriteria(String s) {
    return exactlySevenDigits(s) && containsRepeatedDigits(s);
}

private static boolean exactlySevenDigits(String s) {
    return s.matches("\\d{7}");
}

private static boolean containsRepeatedDigits(String s) {
    return s.matches(".*(\\d)\\1.*");
}

Example results: 结果示例:

3433434  true
6776767  true
9000999  true
1234567  false    (no repeating numbers)
12331233 false    (too long)
123356A  false    (not all digits)

With regex it is a little complicated, I would use this way (Java 8+) instead : 使用正则表达式有点复杂,我会改用这种方式(Java 8+):

boolean check = myString.chars()
                .mapToObj(i -> (char) i)
                .collect(Collectors.toSet())
                .size() == 2;

The idea is to create a Set with the character of this string, if the size of the Set is 2 then it is correct String else it is not. 想法是使用此字符串的字符创建一个Set,如果Set的大小为2,则它是正确的String,否则不是。


Or as Ralf Renz mention in comment, you can use this short way : 或者就像Ralf Renz在评论中提到的那样,您可以使用以下简短方式:

boolean check = myString.chars().distinct().count() == 2;

So your final solution should look like this : 因此,您的最终解决方案应如下所示:

boolean check = myString.matches("\\d{7}") && myString.chars().distinct().count() == 2;

You can do it as follows: 您可以按照以下步骤进行操作:

String str = "3433434";
boolean sevenOf2Digits = str.length() == 7 &&
                         str.matches("(\\d)\\1*+(\\d)(\\1|\\2)*");
System.out.println(sevenOf2Digits);

The first (\\\\d) captures the first digit in group 1. 第一个(\\\\d)捕获组1中的第一个数字。

\\\\1 is a backreference to group 1, so the first digit. \\\\1是对组1的后向引用,因此是第一位。 * is 0 or more of those digits, + makes that possessive, which is required to prevent the next (\\\\d) from matching the same digit. *是0或大于0的这些数字, +使其具有所有格,这是防止下一个(\\\\d)与同一数字匹配所必需的。

The following (\\\\d) captures the second digit in group 2. 以下(\\\\d)捕获组2中的第二个数字。

(\\\\1|\\\\2)* just matches 0 or more of any combination of the first or second digits. (\\\\1|\\\\2)*仅匹配第一个或第二个数字的任意组合中的0个或多个。


I separated out the length check for simplicity. 为了简单起见,我分离出了​​长度检查。 If you want a pure regex solution, you can add the length check to your regex in the form of a lookahead by adding (?=.{7}$) to the start of your regex. 如果您需要纯正则表达式解决方案,则可以通过将(?=.{7}$)到正则表达式的开头,以超前的形式将长度检查添加到正则表达式中。

"(?=.{7}$)(\\d)\\1*+(\\d)(\\1|\\2)*"
    String regex = "[10]{7}|[20]{7}|[21]{7}|[30]{7}|[31]{7}|[32]{7}|[40]{7}|[41]{7}|[42]{7}|[43]{7}|[50]{7}|[51]{7}|[52]{7}|[53]{7}|[54]{7}|[60]{7}|[61]{7}|[62]{7}|[63]{7}|[64]{7}|[65]{7}|[70]{7}|[71]{7}|[72]{7}|[73]{7}|[74]{7}|[75]{7}|[76]{7}|[80]{7}|[81]{7}|[82]{7}|[83]{7}|[84]{7}|[85]{7}|[86]{7}|[87]{7}|[90]{7}|[91]{7}|[92]{7}|[93]{7}|[94]{7}|[95]{7}|[96]{7}|[97]{7}|[98]{7}";

    System.out.println(Pattern.matches(regex, "3433434"));
    System.out.println(Pattern.matches(regex, "6776767"));
    System.out.println(Pattern.matches(regex, "9000999"));

That should do it. 那应该做。 All combinations of two digits with a length of 7. 两位数字的所有组合,长度为7。

(?=^.{7}$)(\\d)\\1*(?!\\1)(\\d)(?:\\1|\\2)*

This should do it. 这应该做。 It finds a digit and repeats, then finds a second digit and repeats. 它找到一个数字并重复,然后找到另一个数字并重复。 Then it checks if the rest of the number is one of those 2. 然后,它检查数字的其余部分是否为2之一。

I'll explain in detail what this does. 我将详细解释它的作用。

(?=^.{7}$) : Before starting, make sure there are 7 characters between the start and end. (?=^.{7}$) :开始之前,请确保开始和结束之间有7个字符。 If shorter or longer, fast fails. 如果较短或较长,则快速失败。

(\\d)\\1*(?!\\1)(\\d) : Get the first digit and save it in a capture group. (\\d)\\1*(?!\\1)(\\d) :获取第一个数字并将其保存在捕获组中。 Then matches if the captured digit is also the next one. 如果捕获的数字也是下一个数字,则匹配。 If there is only a single digit, the next part will catch that. 如果只有一位,那么下一部分将捕获该位。 Last digit should always be different then the first one. 最后一位应始终与第一位不同。

(?:\\1|\\2) : repeat the 2 captured digits 0 or more times. (?:\\1|\\2) :将2个捕获的数字重复0次或更多次。

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

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