Data quality is the most important part of any Salesforce Organisation. So applications works for
salesforce they are nothing but deals with the data. But sometime it works like GIGO “Garbage In Garbage Out”.
If application accepts the garbage data and saved in Salesforce database then we are loosing the data quality.
It means salesforce application should be capable to filter data before saving in the salesforce database.
At this point we requires some validation on the input data. To handle this type of situations best process
it to have some regular expression on the input data.
Regular expressions are a way to describe a set of strings based on common characteristics shared by each
string in the set. They can be used to search, edit, or manipulate text and data.
It uses the Pattern and Mapper classes and there methods for Regular Expression.
Pattern class in Apex is exact copy of Java Pattern class.
The Pattern
class defines an alternate compile
method that accepts a set of flags affecting the way the
pattern is matched. The flags parameter is a bit mask that may include any of the following public static fields:
In short we need to provide the regx in string format to the Patter.complie(Regx String) method.
Until now, we’ve only been interested in whether or not a match is found at some location within a particular
input string. We never cared about where in the string the match was taking place.
You can make your pattern matches more precise by specifying such information with boundary matchers.
For example, maybe you’re interested in finding a particular string, but only if it appears at the beginning or end of a line. Or maybe you want to know if the match is taking place on a word boundary, or at the end of the previous match.
The following table lists and explains all the boundary matchers.
Boundary Construct | Description |
^ | The beginning of a line |
$ | The end of a line |
\b | A word boundary |
\B | A non-word boundary |
\A | The beginning of the input |
\G | The end of the previous match |
\Z | The end of the input but for the final terminator, if any |
\z | The end of the input |
e.g As noted above, ^
matches the beginning of a line, and $
matches the end.
Enter your regex: ^dog$
Enter input string to search: dog
I found the text "dog" starting at index 0 and ending at index 3.
e.g. Salesforce Example
private string GetVersionPhoneNumber(string strTelephone)
{
string strVersionTelephone;
Integer lastEnd=0;
try
{
// Regular expression to get only digit from the Telephone RAW string
// Label.Vcard_TelephoneDigitParserRegx contains the regx string ’[^0-9]‘
Pattern qpEncodingPattern = Pattern.compile(Label.Vcard_TelephoneDigitParserRegx);
strVersionTelephone = qpEncodingPattern.matcher(strTelephone).replaceAll(”);
}
catch(Exception ex){
System.debug(‘Inside the vCard_FieldMap_XformCapture : GetVersionPhoneNumber====>’+ex);
}
return strVersionTelephone;
}