Java – Regular expressions detect if a character is repeated more than three times

Regular expressions detect if a character is repeated more than three times… here is a solution to the problem.

Regular expressions detect if a character is repeated more than three times

I’ve tried following the solution described here: https://stackoverflow.com/a/17973873/2149915
Try to match a string with the following requirements:
– More than 3 characters in the string should be matched and returned in order repeated.

Example:

  • Hello, how are you… -> Effective
  • Hello, hello….-> invalid
  • HIII -> valid
  • HIIIII -> is invalid

Wait, the idea is to detect meaningless text.

My solution so far is to modify the regular expression in the link.

Original:^(?!. *([A-Za-z0-9])\1{2})(?=.*[a-z])(?=.*\d)[A -Za-z0-9]+$

Adaptation: ^(?!. *([A-Za-z0-9\.\,\/\|\\])\1{3})$

Basically I removed the requirement to capture groups of numbers and alphanumeric numbers: (?=.*[a-z])(?=.*\d)[A-Za-z0-9]+ and tried to add extra character detection like ./, \ etc, but it doesn’t seem to match any characters….

Any ideas on how to achieve this?

Thanks in advance 🙂

Edit:
I found this regular expression on this problem https://stackoverflow.com/a/44659071/2149915: ^.*(\ S)(?: ?\1){9,}.*$ and adjust it to match only 3 characters, such as ^.*(\S)(?: ?\1){3}.*$.

Now it detects something like this:

  • aaaa -> is invalid
  • Hello……. -> Invalid
  • …. -> is invalid

But it doesn’t take into account gaps like this :

  • 。 . . . .

Is it possible to achieve this by modification?

Solution

If you are looking for any character that is repeated more than 3 times, I think there is an easier solution :

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

Related Problems and Solutions