Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a regex to find a consecutive repeat in a string (i.e. [12][12]) but only of length 2 or greater?

Tags:

java

regex

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class test {
    public static void main(String[] args) {
        String a ="12341234";
        String regex="^(\\d+?)\\1$";
        Pattern p=Pattern.compile(regex);
        Matcher matcher = p.matcher(a);

        while(matcher.find()) {
            System.out.println(matcher.group(1));
        }
    }
}

By using the Matcher and the regex above, I get any repeat. However, I only want repeats of size 2 or greater. When I tried doing the regex of

^(\\d+?){2,100}\\1$

, nothing was returned when I executed the program even though there was a repeat between length 2 and 100. What am I doing wrong?

like image 848
HSCoder Avatar asked Jun 18 '16 02:06

HSCoder


2 Answers

Your idea is right, you just need to fix a little. Try this:

(\d{2,100})\1

I've tested [here]

Update for HSCoder's request :

make sure that the repeated sequence doesn't have two consecutive numbers that are the same? For example, [11][11] wouldn't work but [12][12] would

Well it's pretty painful to get every two consecutive numbers which are different. Try this :

((?:(?=0[^0]|1[^1]|2[^2]|3[^3]|4[^4]|5[^5]|6[^6]|7[^7]|8[^8]|9[^9])\d)+\d)\1

I've tested [here]

like image 198
meomeomeo Avatar answered Sep 21 '22 13:09

meomeomeo


Description

([0-9]{2,})\1+

Regular expression visualization

This regular expression will do the following:

  • Find substrings consisting of 2 or more digits that repeat 1 or more times in a row.

Example

Live Demo

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

Sample text

22222
1111
2323
12341234
123123123123
123123123
1
11
1212
14
14
12345

Sample Matches

  • Capture group 0 gets the entire matched string include all the repeats
  • Capture group 1 gets the substring that was repeated
[0][0] = 2222
[0][1] = 22

[1][0] = 1111
[1][1] = 11

[2][0] = 2323
[2][1] = 23

[3][0] = 12341234
[3][1] = 1234

[4][0] = 123123123123
[4][1] = 123123

[5][0] = 123123123
[5][1] = 123

[6][0] = 1212
[6][1] = 12

Explanation

NODE                     EXPLANATION
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [0-9]{2,}                any character of: '0' to '9' (at least 2
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  \1+                      what was matched by capture \1 (1 or more
                           times (matching the most amount possible))
----------------------------------------------------------------------
like image 41
Ro Yo Mi Avatar answered Sep 22 '22 13:09

Ro Yo Mi