Sometimes, one needs to write hundreds of lines of code, to process a string and make sure it’s valid (formatted as expected) or transform it into proper format. Some examples of this are validating a string to see if it’s a correct email address, postal code, phone number or URL. Some other examples are grabbing html tags or striping down the XML or HTML tag to get a clear text, trimming the whitespaces, removing duplicate lines or items and many more.
Apex in Force.com platform has just the right set of classes to help you carry out such operations pretty much the same way Java does it.
“A regular expression is a string that is used to match another string, using a specific syntax. Apex supports the use of the regular expression through its Pattern and Matcher classes.” Quoted right from the holly guide. Any regular expression that is written for Java can be used with Apex as well.
In order to utilize these classes we first need to know what each of them does.
Pattern class is designed to contain the regular expression string and you compile the expression into an object of this class. You only need to use this class once. Using this class you will be able to create a Matcher object by passing your string (on which you want to carry out surgery or validation).
pattern myPattern = pattern.compile('(a(b)?)+');
Matcher in turn allows you to do further actions such as checking to see if the string matched the pattern or allows you to manipulate the original string in various ways and produce a new desired one.
matcher myMatcher = myPattern.matcher('aba');
My first example will be how to validate an email address. I personally had some struggles with this since the email addresses can get pretty ugly at times. Imagine this email address:
name.lastname_23@ca.gov.on.com
String InputString = 'email@email.com';
String emailRegex = '([a-zA-Z0-9_\\-\\.]+)@((\\[a-z]{1,3}\\.[a-z]{1,3}\\.[a-z]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})';
Pattern MyPattern = Pattern.compile(emailRegex);
// Then instantiate a new Matcher object "MyMatcher"
Matcher MyMatcher = MyPattern.matcher(InputString);
if (!MyMatcher.matches()) {
// invalid, do something
}
// to validate a password
String RegualrExpression_Password = '((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})';
//image file extention
String RegualrExpression_ImgFileExt = '([^\s]+(\.(?i)(jpg|png|gif|bmp))$)';
//to validate an IP Address
String RE_IP = '^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$';
//date format (dd/mm/yyyy)
String RE_date = '(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)';
//to match links tag "A" in HTML
String RE_ATags = '(?i)<a([^>]+)>(.+?)</a>';
Another way that you can benefit from the Matcher class to to reformat the string.
Below is an example that shows you, how you can strip the HTML tags from a string and extract the plain text. This is very useful when you want to record email contents into Salesforce or covert the HTML version of an email into it's plain text counterpart.
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('');
For complete reference of Java regular expressions please refer to: here
I am really struggling to grasp regular expressions. Is it possible to search a string and find a match for something like this:
ReplyDeleteFrom: John Doe [mailto:
"From:" and "[mailto:" will always be static but "John Doe" could change.
I think this might work.
ReplyDeleteFrom.*\[mailto:
in your HTML stripping example why do you need these 2 lines? Won't the first line suffice?
ReplyDeletestring result = html.replaceAll('
', '\n');
result = result.replaceAll('
', '\n');
This way you can convert the HTML break lines into text "newline" character.
ReplyDeleteYou've already replaced all the br tags with "newline" tags in the first statement, right? So why have the second line that makes no change?
ReplyDeleteThe first and second statements have a slight difference to cover the scenario where the HTML developer have a space between the "BR" and the closing tag.
ReplyDeleteOh, wait... there's a space between the r and slash so the replacements aren't the same. NeverMind!
ReplyDeleteHi Sam this might be what I was looking for...we have an Apex class in salesforce to process email body into fields, but it only processes plain text emails. We have an HTML email which gives an error. If we use your HTML stripping code will this covert the HTML to plain text and then process email correctly?
ReplyDeleteSam - thank you so much for posting this! A wealth of information for a novice like me! :)
ReplyDeleteTwo things you may want to consider:
ReplyDeletePrepend your regular expression with (?i) to make the match case-insensitive so to ensure that any html br tags that are in caps are also replaced:
result = result.replaceAll('(?i)
', '\n');
To match br tags that are and are't self closing, and match any arbitrary number of spaces inside the tag:
result = result.replaceAll('(?i)', '\n');
Regular expressions are very... exacting. Also, if you want to do anything more complex than this with an html document, you should probably use a language parser rather than regular expressions because regexes can't readily describe a language grammar well enough to eliminate all edge cases.
Sam,
ReplyDeleteI have not been successful in using split function on the matcher object as documented here http://download.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html#split(java.lang.CharSequence, int)
Do you know if there is a workaround?
My use case is, I have an email chain and am trying to identify the second occurrence of 'From:'. If I had split function then I can split the email chain into two.
Thanks in adv
SK
Have you found a way to use the Pattern flags like you can with Java? Everytime I try to use a flag like Pattern.MULTILINE I get a variable does not exist error.
ReplyDeleteHey Sam ,
ReplyDeleteIs there anything wrong with the following code segment, i m always getting "invalid" :-(
String InputString = 'PO';
String emailRegex = '[A-Z]';
Pattern MyPattern = Pattern.compile(emailRegex);
Matcher MyMatcher = MyPattern.matcher(InputString);
if (!MyMatcher.matches()) {
system.debug('-------- invalid');
}
else{
system.debug('-------- valid');
}
Akhilesh Soni, Java documention says about matches():
ReplyDelete"Attempts to match the entire region against the pattern".
So in you case it's always false, because 'PO' (the whole string) is not a letter between A-Z.
help in validate a number with single "-"
ReplyDeleteIn addition to this, I've read your other article on GoogleCharts. Thank you very much for sharing your knowledge.
ReplyDeletehow to avoid names like 'a namika' or 'a......nnu' in text field Can u plz tell me the regex of this.
ReplyDeleteI have To append a hardcoded text on an RE how can i do this
ReplyDeleteExample
Test Date 2012-2-3
Test Date 2011-4-5
...
I have to append "Test Date" in my date RE, how can i accomplish this