Skip to main content
Formatted code.
Source Link
Daniel Ballinger
  • 103k
  • 40
  • 275
  • 601

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); system.debug(m.find()); System.debug(m.matches()); if (m.find()) { system.debug('Duplicate character ' + m.group(1)); }

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

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

Regards, Rong

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); system.debug(m.find()); System.debug(m.matches()); if (m.find()) { system.debug('Duplicate character ' + m.group(1)); }

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

Regards, Rong

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);
system.debug(m.find());
System.debug(m.matches());
if (m.find())
{
    system.debug('Duplicate character ' + m.group(1));
}

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

Regards, Rong

Source Link
Rong
  • 31
  • 2

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); system.debug(m.find()); System.debug(m.matches()); if (m.find()) { system.debug('Duplicate character ' + m.group(1)); }

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

Regards, Rong