Regex Matcher In Apex

The blow code is replacing the SSN number with * from a sentence.

Sample Code:

String searchText = 'This is your SSN 000-45-6789 number';
Pattern objPattern = Pattern.compile('[0-9]{3}-[0-9]{2}-[0-9]{4}');
String result = objPattern.matcher(searchText).replaceAll('***-**-****');
System.debug('searchText-' + searchText);
System.debug('result-' + result);

1 Star2 Stars3 Stars4 Stars5 Stars (6 votes, average: 4.17 out of 5)

About Biswajeet

Biswajeet is my Name, Success is my Aim and Challenge is my Game. Risk & Riding is my Passion and Hard Work is my Occupation. Love is my Friend, Perfection is my Habit and Smartness is my Style. Smiling is my Hobby, Politeness is my Policy and Confidence is my Power.