Sai Kiran
Sai Kiran

Reputation: 31

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 .

I want to match these numbers from java .

example numbers:

3433434
6776767
9000999

Please help to create the regular expression for these pattern numbers

Upvotes: 0

Views: 1190

Answers (5)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59950

With regex it is a little complicated, I would use this way (Java 8+) instead :

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.


Or as Ralf Renz mention in comment, you can use this short way :

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;

Upvotes: 1

mattmess1221
mattmess1221

Reputation: 4434

(?=^.{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.

I'll explain in detail what this does.

(?=^.{7}$): Before starting, make sure there are 7 characters between the start and end. If shorter or longer, fast fails.

(\d)\1*(?!\1)(\d): Get the first digit and save it in a capture group. 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.

Upvotes: 0

Bernhard Barker
Bernhard Barker

Reputation: 55589

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.

\\1 is a backreference to group 1, so the first digit. * is 0 or more of those digits, + makes that possessive, which is required to prevent the next (\\d) from matching the same digit.

The following (\\d) captures the second digit in group 2.

(\\1|\\2)* just matches 0 or more of any combination of the first or second digits.


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}$)(\\d)\\1*+(\\d)(\\1|\\2)*"

Upvotes: 1

ᴇʟᴇvᴀтᴇ
ᴇʟᴇvᴀтᴇ

Reputation: 12751

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)

Upvotes: 1

Ralf Renz
Ralf Renz

Reputation: 1061

    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.

Upvotes: -1

Related Questions