1

My Regex:

^(\d)\1{2}.\1{3}.\1{3}-\1{2}$

Repetitions that aren't allowed:

000.000.000-00
111.111.111-11
222.222.222-22
333.333.333-33
444.444.444-44
555.555.555-55
666.666.666-66
888.888.888-88
999.999.999-99

It's working fine according to https://www.regex101.com/

So I'm trying to put it in Java, I already tried this way: ^(\\d)\\1{2}.\\1{3}.\\1{3}-\\1{2}$, but definitely don't want to work.

My code:

if (hasTheSameDigits(cpfReplaced)) {
    msg = "All the digits of informed CPF are equal.";
}

public boolean hasTheSameDigits(String cpf) {
     return cpf.matches("^(\\d)\\1{2}\\1{3}\\1{3}\\1{2}$");
}

It would be great if someone could help me.

developer033
  • 24,267
  • 8
  • 82
  • 108
  • regex101 doesn't support the Java flavor of regular expressions. Better test with [RegexPlanet](http://www.regexplanet.com/advanced/java/index.html). – RealSkeptic Nov 01 '15 at 17:59
  • 4
    Also note that your regex would also match `111A111B111-11`. – RealSkeptic Nov 01 '15 at 18:04
  • 1
    *"I already tried this way: ^(\\d)\\1{2}.\\1{3}.\\1{3}-\\1{2}$, but definitely don't want to work."* can you post your code? I don't see reason why this would not work if used properly. – Pshemo Nov 01 '15 at 18:08
  • In that site you said, it gives me the regex that I said "^(\\d)\\1{2}.\\1{3}.\\1{3}-\\1{2}$". Yes, it's a problem, but it will never happen, because that field has a mask that only accept digits – developer033 Nov 01 '15 at 18:12
  • @developer033 I'm confused: the listed repetitions shall be allowed or not? – yasd Nov 01 '15 at 18:14
  • @developer033 Supposed the listed repetitions shall be allowed - as I suspect - the regex works fine for me, but the fact that RealSkeptic mentioned. I'll post my code, maybe that helps. – yasd Nov 01 '15 at 18:17
  • @yasd I suspect that purpose of this regex is to find things which we should not allow. So OP code can look like `boolean isValidNumber(String number){ return !number.matches(regex);}`. – Pshemo Nov 01 '15 at 18:18
  • Guys, I managed to make it work, it was just a little mistake, I was removing the mask from the field in another method and I didn't notice that. It's working fine, btw anyone could tell me what can I do to matches only digits, not the letters like "111A111B111-11". – developer033 Nov 01 '15 at 18:21
  • @yasd "*I'll post my code, maybe that helps*" please don't post answers that states "*I can't reproduce your problem/your code works for me*". That is not what Stack Overflow is for. We want to have solutions for question which are reproducible. If question doesn't provide enough informations then it doesn't belong to Stack Overflow since it can't help anyone in the future. More info: [Are “works for me” answers valid?](http://meta.stackexchange.com/questions/118992/are-works-for-me-answers-valid) – Pshemo Nov 01 '15 at 18:22
  • 1
    @Pshemo I thought adding real code might help - but okay. @developer033 `Pattern.compile("^(\\d)\\1{2}\\.\\1{3}\\.\\1{3}-\\1{2}$")` contains the dot correction RealSkeptic mentioned and `pattern.matcher("000.000.000-00").matches()` brings true for all strings in your list. – yasd Nov 01 '15 at 18:26
  • @yasd Now that OP edited his question and added code which lets us see what went wrong feel free to post answer explaining mistakes which ware made (like lack of `.` support). Feel free to also include some optimizations like precompiling regex once and reusing it in method like you did in your comment: `pattern.matcher("000.000.000-00").matches()`. – Pshemo Nov 01 '15 at 18:34
  • Now that I think more about this regex, I found a better solution, since I'm already replacing the non-digits characters, I can just simply change my regex to: `"^(\\d)(\\1){10}$"` [See it](http://rubular.com/r/JVD4JEDkxe) – developer033 Nov 01 '15 at 18:42
  • Let me guess: http://stackoverflow.com/a/123681/1393766 :) – Pshemo Nov 01 '15 at 18:44
  • @Pshemo [here](http://stackoverflow.com/questions/31880153/find-repeated-numbers-sequence-with-regex?rq=1) :) – developer033 Nov 01 '15 at 18:47

1 Answers1

1

This regex should work: ([0-9])\1\1\.\1\1\1\.\1\1\1-\1\1\1.

(Note: it bloats to this Java String because of the escapes: "([0-9])\\1\\1\\.\\1\\1\\1\\.\\1\\1\\1-\\1\\1\\1".)

Explanation:

([0-9]) finds a digit.

\1 finds the same digit. And again. And Again. AND AGAIN.

\. matches a .

- matches a -

Laurel
  • 5,965
  • 14
  • 31
  • 57