[SalesForce] SFDC regular expression to catch the word with repeated characters

I am going to use the regular expression to catch the word with repeated characters (letter, digit, or underscore).
I found pretty interesting results during my testing. If I am using apex code, Matcher.matches() would not catch it, while Matcher.find() would return correct value. If I am trying the REGEX() in the formula field, it never works out.
The pattern I am using is '(\w)\1+'. Just wonder how to get REGEX() worked for this requirement.

//Pasted the Apex code for the reference.
String stringToMatch = 'abcccdefg';  // String stringToMatch = 'abcdefg';
Pattern p = Pattern.compile('(\\w)\\1+');
Matcher m = p.matcher(stringToMatch);
if (m.find())
    system.debug('Duplicate character ' + m.group(1));

Any help would be truly appreciated. Thank you very much in advance.


Best Answer

The formula would work, except that it automatically includes ^ and $ boundaries by default around the string. The solution is to add wildcard matches on either side of the expression. The following rule should work:

REGEX(Name, ".*(\\w)\\1+.*")

For matchers, you should ordinarily use find() if you expect multiple results. matches() instead considers the entire string. See Using Match Operations.

  • Match the Matcher object's entire input string against the pattern using the matches method
  • Match the Matcher object's input string against the pattern, starting at the beginning but without matching the entire region, using the lookingAt method
  • Scan the Matcher object's input string for the next substring that matches the pattern using the find method
Related Topic