Regular expression to match any character being repeated more than 10 times
The regex you need is /(.)\1{9,}/
.
Test:
#!perl
use warnings;
use strict;
my $regex = qr/(.)\1{9,}/;
print "NO" if "abcdefghijklmno" =~ $regex;
print "YES" if "------------------------" =~ $regex;
print "YES" if "========================" =~ $regex;
Here the \1
is called a backreference. It references what is captured by the dot .
between the brackets (.)
and then the {9,}
asks for nine or more of the same character. Thus this matches ten or more of any single character.
Although the above test script is in Perl, this is very standard regex syntax and should work in any language. In some variants you might need to use more backslashes, e.g. Emacs would make you write \(.\)\1\{9,\}
here.
If a whole string should consist of 9 or more identical characters, add anchors around the pattern:
my $regex = qr/^(.)\1{9,}$/;
Regex to validate 3 repeating characters
The regex solution for this is very inefficient. Please consider treating this answer from pure academic interest.
The pattern that fails strings having 4 or more occurrences of the same char is
^(?!.*(.).*\1.*\1.*\1).*
The last .*
may be replaced with a more restrictive pattern if you need to precise this pattern.
See the regex demo.
The main part here is the (?!.*(.).*\1.*\1.*\1)
negative lookahead. It matches
any 0+ chars (if Pattern.DOTALL
is used, any char including newlines), as many as possible, then it matches and captures (with (.)
) any char into Group 1, and then matches any 0+ chars followed with the same char 3 times. If the pattern is found (matched), the whole string match fails.
Why is it inefficient? The pattern relies heavily on backtracking. .*
grabs all chars to the end of the string, then the engine backtracks, trying to accommodate some text for the subsequent subpatterns. You may see the backtracking steps here. The more .*
there is, the more resource-consuming the pattern is.
Why is lazy variant not any better? The ^(?!.*?(.).*?\1.*?\1.*?\1).*
looks to be faster with some strings, and it will be faster if the repeating chars appear close to each other and the start of the string. If they are at the end of the string, the efficiency will degrade. So, if the previous regex matches 121212
in 77 steps, the current one will also take the same amount of steps. However, if you test it against 1212124444
, you will see that the lazy variant will fail after 139 steps, while the greedy variant will fail after 58 steps. And vice versa, 4444121212
will cause the lazy regex fail quicker, 14 steps vs. 211 steps with the greedy variant.
In Java, you may use it
s.matches("(?!.*(.).*\\1.*\\1.*\\1)")
or
s.matches("(?!.*?(.).*?\\1.*?\\1.*?\\1)")
Use Jacob's solution in production.
.NET Regular Expression allow 3 repeated characters
This regex will work
^(?=.{8,15}$)(?!.*?(.)\1{3})[A-Za-z0-9]+$
Regex Demo
Regex Breakdown
^ #Start of string
(?=.{8,15}$) #Lookahead to check there are 8 to 15 digits
(?!.*?(.)\1{3}) #Lookahead to determine that there is no character repeating more than thrice
[A-Za-z0-9]+ #Match the characters
$ #End of string
For unicode
support, you can use
^(?=.{8,15}$)(?!.*?(.)\1{3})[\p{L}\p{N}]+$
NOTE :- For matching one character and one digit, you can use
^(?=.{8,15}$)(?=.*[A-Za-z])(?=.*\d)(?!.*?(.)\1{3})[A-Za-z0-9]+$
Regex Demo
Regex to detect if character is repeated more than three times
I think there's a much simpler solution if you're looking for any character repeated more than 3 times:
String[] inputs = {
"hello how are you...", // -> VALID
"hello how are you.............", // -> INVALID
"hiii", // -> VALID
"hiiiiii" // -> INVALID
};
// | group 1 - any character
// | | back-reference
// | | | 4+ quantifier including previous instance
// | | | | dot represents any character,
// | | | | including whitespace and line feeds
// | | | |
Pattern p = Pattern.compile("(.)\\1{3,}", Pattern.DOTALL);
// iterating test inputs
for (String s: inputs) {
// matching
Matcher m = p.matcher(s);
// 4+ repeated character found
if (m.find()) {
System.out.printf(
"Input '%s' not valid, character '%s' repeated more than 3 times%n",
s,
m.group(1)
);
}
}
Output
Input 'hello how are you............. not valid', character '.' repeated more than 3 times
Input 'hiiiiii' not valid, character 'i' repeated more than 3 times
Input 'hello how are you' not valid, character ' ' repeated more than 3 times
Regex expression to check if contains more than 2 repeated characters
If I understand correctly what you are considering to be invalid, you want this:
/(.)\1{2,}/
Find repeating characters in a string using regex
(.)\1{2}
(.) matches any char
\1 matches that exactly char
{2} is to grant its 2 more of that
Python 3 regex searching for repeating characters function is not working
Try this ^(?!.*(.).*\1)[a-zA-Z0-9]{10}$
https://regex101.com/r/XeaaTR/1
^
(?! # No repeating chars
.*
( . ) # (1)
.* \1
)
[a-zA-Z0-9]{10} # 10 alnum's
$
Related Topics
How to Call Methods Simultaneously in Java
Enter Array Without Knowing Its Size
How to Remove Trailing Zeros from a Double
Maven Build Cannot Find Symbol When Accessing Project Lombok Annotated Methods,
Regular Expression for Excluding Special Characters
How to Convert Minutes to Hours and Minutes (Hh:Mm) in Java
Error Says It's Dividing by Zero While It's Not Initialised as Zero
Null Pointer on an Autowired Bean Which Is Not Mocked by Mockito
Spring Rest - Create Zip File and Send It to the Client
How to Display Auto-Configuration Report When Running a Spring Boot Application
How to Get the Average of a 2D Array
Setonclicklistener() on a Null Object Reference Error
Maven Builds Are Working, But Junit Is Throwing a Nosuchmethoderror
Error Starting Applicationcontext in Spring Boot App
How to Replace Multiple Words in a Single String in Java
How to Know If All the Variables in a Class Are Null
How to Disable Error Highlighting in VS Code
Java Regex to Validate Full Name Allow Only Spaces and Letters