Patterns and Matchers in Apex #inSalesforce

Patterns and Matchers in Apex #inSalesforce


This is to search text using regular expressions. A pattern is a compiled representation of a regular expression which are used by matchers to perform match operations on a character string.


Example1:
Matcher theMatcher = thePattern.matcher('aaaaab');
System.assert(theMatcher.matches());
Pattern thePattern = Pattern.compile('a*b');

The above can be written as:

Boolean Test = Pattern.matches('a*b', 'aaaaab');


Example2:
String theString = 'Phone Number 022-44-8976 number';
Pattern thePattern = Pattern.compile('[0-9]{3}-[0-9]{2}-[0-9]{4}');
String theResult = thePattern.matcher(theString).replaceAll('***-**-****');
System.debug('!!!!theString!!!!!' + theString);
System.debug('!!!!theResult!!!!' + theResult);


Visual: https://www.youtube.com/watch?v=mWWRBXYF_2I

Comments