What is the purpose of REGEX function in Salesforce?

0 votes
186 views
Sophia Morrow asked 07-Feb-2018 in SalesForce by Sophia Morrow

What is the purpose of REGEX function in Salesforce?

1 Answer

0 votes
Tarun Gautam answered 19-Mar-2018 by Tarun Gautam

In Salesforce Regex is a function that is used in validation rules and it is used to specify a format. Regex expressions are the Regular Expressions to increase the quality and maintenance of data of our customers or the organizations based on the patterns.

Regex expressions or we can say it as Regular expressions are the type of texts or string for describing a predefined pattern as per the use case.
Ex: if there is a Mobile Number named field, then it is clear that we must use the number in it as input, but now the question occurs how can be validated the Mobile Number field, the answer is using the regex expressions or regular expressions.

The Regular expression also called Regex or Regexp and sometimes Rational Expression. A sequence of characters defines a search pattern and usually these patterns used by the string searching algorithms for “find” or “find and replace” operations on strings.

There are some of the common Regex expressions:

  • Boundary Matchers
    • ^ - it represents the beginning of the line.
    • $ - it represents the end of the line.
    • * - it matches to zero or more of the previous character.


  • Character-class operators
    • \x – it is a literal escape
    • a-z – it is displaying Range
    • […] – it represents Grouping
    • [a-e][i-u] – it represents Union
    • [a-z&&[AEIOU]] – it represents Intersection


  • Line Terminators
    • \n – it is used for the newline character

Here is an example in which all the above Regex expressions are used in one single program:

public class RegexValidator

{
 public static Boolean CheckEmail (String str)
  {
  return Pattern.compile(‘([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})’).matcher(str).matches();
  }
}

Regular expressions are used in different ways according to our need and the use case, and it has its scope to maintain data quality whether used in case of a validation rule or a custom code. Some of the cases regex can be used to validate some of the data before saving.