41

Can anyone help me or direct me to build a regex to validate repeating numbers

eg : 11111111, 2222, 99999999999, etc

It should validate for any length.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
Charmila
  • 415
  • 1
  • 4
  • 5
  • 3
    Are you just looking for a single digit repeating, or would any sequence of digits repeating count. E.g. is 123123123123 repeating for you? – borrible Jun 28 '11 at 14:23
  • just single digit repeating, I actually need the opposite of that. I want to identify single digit repeating as invalid – Charmila Jun 28 '11 at 14:28
  • I need to find the match like- 000000000,111111111,222222222, like till 9 which will repeats exactly 9 times and also this string -> "123456789". For this type of pattern how should I need to write the regex in java. Any solution . – chandru Aug 25 '22 at 14:30

6 Answers6

79
\b(\d)\1+\b

Explanation:

\b   # match word boundary
(\d) # match digit remember it
\1+  # match one or more instances of the previously matched digit
\b   # match word boundary

If 1 should also be a valid match (zero repetitions), use a * instead of the +.

If you also want to allow longer repeats (123123123) use

\b(\d+)\1+\b

If the regex should be applied to the entire string (as opposed to finding "repeat-numbers in a longer string), use start- and end-of-line anchors instead of \b:

^(\d)\1+$

Edit: How to match the exact opposite, i. e. a number where not all digits are the same (except if the entire number is simply a digit):

^(\d)(?!\1+$)\d*$

^     # Start of string
(\d)  # Match a digit
(?!   # Assert that the following doesn't match:
 \1+  # one or more repetitions of the previously matched digit
 $    # until the end of the string
)     # End of lookahead assertion
\d*   # Match zero or more digits
$     # until the end of the string
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • 1
    For this to match, you'd require at least 2 of the same digit. You should use * rather than +. – Neil Jun 28 '11 at 14:28
  • 1
    @Neil: Correct, that's why I wrote "If `1` should also be a valid match[...]". – Tim Pietzcker Jun 28 '11 at 14:30
  • Thanks it's working, how do I achieve the opposite of this? I want to identify single digit repeating as invalid – Charmila Jun 28 '11 at 14:32
  • Can't you apply the regex and invert the result of the match? – Tim Pietzcker Jun 28 '11 at 14:35
  • In fact, 1 is a valid match. He said so himself.. "It should validate for any length." – Neil Jun 28 '11 at 14:37
  • @Neil: The question is whether she really means what she wrote :) – Tim Pietzcker Jun 28 '11 at 14:40
  • @Tim: +1 for that. Welcome to my world. – Neil Jun 29 '11 at 07:55
  • @Neil: My sympathies. I'm a doctor (and a hobby programmer), and I once was responsible for "translating" between doctors and programmers in a large medical e-learning project. It's unbelievably hard. Users never know what they really want, and even the most intelligent people have a capability to abstract their mental image of what a program should do for them into terms someone who has to put that into code can work with... – Tim Pietzcker Jun 29 '11 at 08:34
  • @Tim: Exactly. In my experience, most people cannot extrapolate a task into its individual steps unless they've done it themselves and as it happens to be with programming, most users have never done anything similar. It'd be like someone asking you to clean their car and then they scratch their heads when you need a rag. – Neil Jun 29 '11 at 09:11
  • I am looking for a regex to validate if string does not contain same number consecutively 7-10 times. I tried using the above regex ^(\d)(?!\1+$)\d*$ but it did not help. Please help. – theEternalStudent Mar 27 '23 at 12:37
  • @Tim Pietzcker, could you please help with above query. – theEternalStudent Mar 27 '23 at 14:42
  • @theEternalStudent: Why not write this as an actual question? This may be more complex than can comfortably be explained in a comment. For example, am I understanding correctly that a string with a digit repeated 11 times would be okay again (only 7 through 10 repeats would be "bad")? – Tim Pietzcker Mar 29 '23 at 06:52
  • I am checking if below condition doesn't not match to avoid consecutive same numbers. I was thinking if a number repeats for 11 then it would have already satisfied the match for 10 consecutive and it should fail. ([0-9])\\1{6}|([0-9])\\1{7}|([0-9])\\1{8}|([0-9])\\1{9} – theEternalStudent Mar 31 '23 at 14:44
  • i did not post it as a question thinking it would be marked as a duplicate. – theEternalStudent Mar 31 '23 at 14:50
  • Well, then why not simply check if a number is repeated 7 times and be done with it? If `(\d)\1{6}` can be found in the string (be careful to use the regex to search in the string, not to match the entire string), then there are at least 7 identical consecutive digits in the string. – Tim Pietzcker Apr 05 '23 at 19:27
15

To match a number of repetitions of a single digit, you can write ([0-9])\1*.

This matches [0-9] into a group, then matches 0 or more repetions (\1) of that group.

You can write \1+ to match one or more repetitions.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
9

Use a backreference:

(\d)\1+

Probably you want to use some sort of anchors ^(\d)\1+$ or \b(\d)\1+\b

mhyfritz
  • 8,342
  • 2
  • 29
  • 29
7

I used this expression to give me all phone numbers that are all the same digit.

Basically, it means to give 9 repetitions of the original first repetition of a given number, which results in 10 of the same number in a row.

([0-9])\1{9}

Mimi Mosa
  • 71
  • 1
  • 1
1

(\d)\1+? matches any digit repeating

ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
0

you can get repeted text or numbers easily by backreference take a look on following example:

regexr.com

this code simply means whatever the pattern inside [] . ([inside pattern]) the \1 will go finding same as inside pattern forward to that.

iBug
  • 35,554
  • 7
  • 89
  • 134