0

I have created regular expression for checking consecutive three repeated characters.It is working fine while testing on Online regex tester but same expression doesn't seem to work when I implemented it in code. What's wrong with my implementation?

   Pattern pattern = Pattern.compile("([a-z\\d])\\1\\1");

   Matcher matcher = pattern.matcher(s);
    if(matcher.matches()){
        return false;
    }
    else
    return true; 
Milad Yarmohammadi
  • 1,253
  • 2
  • 22
  • 37
Bhuvi
  • 51
  • 2
  • 13
  • What is your input and what errors are you getting? – Shaishav Jogani Sep 03 '16 at 10:13
  • @ShaishavJogani If i pass a string with repeated characters like "aaaabcefg" it is not able to detect it and returns true always – Bhuvi Sep 03 '16 at 10:21
  • `matches()` returns true if and only if whole input string is matched. Otherwise you should use `matcher.find()` or modify your regex like this: `".*?([a-z\\d])\\1\\1.*"` (Note there shouldn't be line-breaks in input or you need to set `s` modifier) – revo Sep 03 '16 at 10:43
  • 2
    I think this answer summarizes it pretty well. http://stackoverflow.com/questions/4450045/difference-between-matches-and-find-in-java-regex – Deendayal Garg Sep 03 '16 at 10:49
  • @revo Can u post it as answer so tht i can accept it? – Bhuvi Sep 05 '16 at 08:46

0 Answers0