17

I want to match something like aaaa, aaaad, adjjjjk. Something like ([a-z])\1+ was used to match the repeated characters, but I am not able to figure this out for four letters.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anonymous user
  • 173
  • 1
  • 1
  • 4
  • Hey guys cool answers but i have one concern "ffffffff".matches("([a-z])\\1{3,}") = true "fffffasdf".matches("([a-z])\\1{3,}") = false "asdffffffasdf".matches("([a-z])\\1{3,}") = false what can i do for the bottom two? – Anonymous user Apr 12 '10 at 14:34

4 Answers4

25

You want to match a single character and then that character repeated three more times:

([a-z])\1{3}

Note: In Java you need to escape the backslashes inside your regular expressions.


Update: The reason why it isn't doing what you want is because you are using the method matches which requires that the string exactly matches the regular expression, not just that it contains the regular expression. To check for containment you should instead use the Matcher class. Here is some example code:

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

class Program
{
    public static void main(String[] args)
    {
        Pattern pattern = Pattern.compile("([a-z])\\1{3}");
        Matcher matcher = pattern.matcher("asdffffffasdf");
        System.out.println(matcher.find());
    }
}

Result:

true
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
18

Not knowing about the finite repetition syntax, your own problem solving skill should lead you to this:

([a-z])\1\1\1

Obviously it's not pretty, but:

  • It works
  • It exercises your own problem solving skill
  • It may lead you to deeper understanding of concepts
    • In this case, knowing the desugared form of the finite repetition syntax

I have a concern:

  • "ffffffff".matches("([a-z])\\1{3,}") = true
  • "fffffasdf".matches("([a-z])\\1{3,}") = false
  • "asdffffffasdf".matches("([a-z])\\1{3,}") = false

What can I do for the bottom two?

The problem is that in Java, matches need to match the whole string; it is as if the pattern is surrounded by ^ and $.

Unfortunately there is no String.containsPattern(String regex), but you can always use this trick of surrounding the pattern with .*:

"asdfffffffffasf".matches(".*([a-z])\\1{3,}.*") // true!
//                         ^^              ^^
Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • If you are trying to match a string where the string you look for is just before an end of line character, the pattern above won't match. (I learnt that the hard way!). You could do: '"asdfffffffffasf".matches(".*([a-z])\\1{3,}(?s).*")' where '(?s)' triggers the matching for end of line chars. – seinecle Mar 11 '13 at 10:10
  • it is best to find character repeating. – Brownsoo Han Feb 02 '17 at 07:32
  • I have been looking for this since last 2 hours. Thank you very much – Parth Bhoiwala Jan 29 '18 at 04:24
6

You can put {n} after something to match it n times, so:

([a-z])\1{3}
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
3

General regex pattern for predefinite repetition is {4}.

Thus here ([a-z])\1{3} should match your 4 chars.

instanceof me
  • 38,520
  • 3
  • 31
  • 40