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?
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]
([0-9]{2,})\1+
This regular expression will do the following:
Live Demo
https://regex101.com/r/cA5qB7/1
Sample text
22222
1111
2323
12341234
123123123123
123123123
1
11
1212
14
14
12345
Sample Matches
[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
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))
----------------------------------------------------------------------
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With