+ Start a Discussion

ReplaceAll Help - Replace String with a Mask

Hi all,

I am trying to mask certain details in the body of an email message using regex and the string method replaceall. I've no issue with the regex, I can see it matching and can add a value to the body with a match. However I have not been able to replace the matched substring, this is what I am trying at the moment:

String mask = '*';
String embody = 'Test text body 1111 test';

Pattern p = Pattern.Compile('(\\d{4})'); //simplified
As I mentioned I am confident that the regex is matching by using debug comments and I have updated the field on a match but I cannot get replaceall to work :(

What I am missing?

In case it is relevent I am trying to use a before insert trigger
The below post have examples and descriptions, please see if this helps

I'm not sure if does...I have no issue with matching but not having much luck with replaceall actually doing anything. I'll try using the last example as a source (for completeness below):

string html = 'your html code';
 //first replace all <BR> tags with \n to support new lines
string result = html.replaceAll('<br/>', '\n');
result = result.replaceAll('<br />', '\n');

//regular expression to match all HTML/XML tags
string HTML_TAG_PATTERN = '<.*?>';

// compile the pattern     
pattern myPattern = pattern.compile(HTML_TAG_PATTERN);

// get your matcher instance
matcher myMatcher = myPattern.matcher(result);

//remove the tags     
result = myMatcher.replaceAll('');