Salesforce REGEX Function

The Salesforce REGEX function is used to check whether the text you entered matches the regular expression rule.

This REGEX is one of the common functions for maintaining the accuracy of business data by setting up certain syntax rules. Whether you are a developer, administrator, or consultant, the function is very important as it helps to improve data quality.

If you need help understanding the REGEX function in Salesforce, I am here your Salesforce Developer, to help you.

In this Salesforce tutorial, I will explain the REGEX function, including its syntax and considerations. Moreover, I will guide you step-by-step through how you can use this function in validation rules to enforce specific data entry formats with real business use case scenarios in both the Salesforce Lightning and Salesforce Classic user interfaces.

REGEX Function in Salesforce

What are Regular Expressions

Regular Expressions (short form known as Regex) are sequences of characters and rules that define a search pattern. They are used for searching, extracting, manipulating, and pattern-matching text with strings.

It is supported in many programming languages, text editors, and tools, including Salesforce. The regular expression works as follows:

  1. Find textual patterns within the text.
  2. Verify whether a string matches a specific pattern.
  3. Do the needful as per your requirement.

REGEX Function in Salesforce

The REGEX function is an advanced Salesforce validation function that ensures that the text entered into the fields follows specific criteria.

REGEX function in Salesforce compares the text field to a regular expression and returns TRUE if there is any match. Otherwise, it returns FALSE. The function helps in pattern matching and text manipulation of the text and enhances work with data.

Syntax of the REGEX Function in Salesforce

The following is the syntax of the REGEX function in Salesforce:

REGEX(text, regex_text)

The REGEX function in Salesforce takes two arguments, which are as follows:

  • text: It is the text field that you want to validate.
  • regex_text: It is the regular expression pattern to match against.

Return: REGEX function returns TRUE if the pattern is found in the text field; otherwise, FALSE.

Important tips to be considered while using the REGEX Function in Salesforce

The following are the considerations that you must remember while working with the REGEX function of Salesforce:

  • In Salesforce, the regular expression syntax is based on Java Platform SE 6 syntax.
  • The backslash (\) character is used as an escape character within strings in Salesforce.
  • Always replace the backslash characters (\) with the double backslashes (\\) characters in Salesforce.
  • In Salesforce, the regular expression engine matches an entire string instead of searching for a match within a string. This means that the regular expression must match the entire string for a successful match to occur.
  • In Salesforce, capture groups and substitutions are ignored. This means you cannot capture specific parts of the matched text or perform substitutions based on matched patterns within Salesforce regular expressions.
  • The REGEX function is available in workflow rules, validation rules, process builder, etc within Salesforce, except for formula fields and custom buttons and links.

Example to understand Regular Expressions used in formulas in Salesforce

See also  MOD() Function in Salesforce | Calculate remainder in Salesforce

Let’s explore an example of how regular expressions can be used in Salesforce.

Suppose an organization that uses Salesforce wants to ensure that all product codes follow a specific pattern for consistency and easy identification of data.

The organization wants the product code to consist of three uppercase letters, a hyphen, and three digits. For that, the organization creates the validation rule using the REGEX function, which is as follows:

REGEX(ProductCode, "^[A-Z]{3}-\\d{3}$")

Here is a detailed explanation:

  • REGEX(): The Salesforce function is used for regular expression matching.
  • ^: It is the symbol that represents the start of the line.
  • [A-Z]{3}: It matches exactly three uppercase letters.
  • -: It matches the hyphen.
  • \d{3}: It matches exactly three digits.
  • $: It matches the end of the line.

With this, you have learned about the REGEX function with its syntax, consideration, and example.

How to use the REGEX function in Salesforce Lightning

Let’s learn how to use the REGEX function in Salesforce Lightning in the validation rule.

Take a scenario in which the company using Salesforce wants to enforce a standard format for industry codes in the account object.

1. Log in to your Salesforce account in the Lightning Interface, click on the “Gear” icon, and then click the “Setup” option to navigate to the “Setup Home” page.

2. Go to the “Object Manager” at the top of the navigation bar, and select the object where you want to apply the validation rule.

According to the scenario, search and choose the “Account” object.

Salesforce REGEX Function

3. On the object manager page of the account object on the left sidebar, click “Validation Rules”. After that, click the “New” button on the right of the page.

Salesforce Lightning REGEX Function

4. Enter the rule name and description for your validation rule. Make sure to check the “Active” checkbox to make this validation rule in use.

According to the scenario, enter the “Rule Name” as “Industry Code Validation”.

REGEX Function in Salesforce Lightning

5. In the formula editor, write a formula that checks if the industry code field follows the specified format as the industry code field contains one of the following options (A, B, C, D) followed by six digits.

To create the Salesforce formula, follow the below steps:

  • Move to the “Function” section on the right side of the page.
  • Expand the “All Function Categories” dropdown.
  • Choose the “Advance” category from the list.
  • Select the “REGEX” function from the list of functions.
  • Click on the “Insert Selected Function” button under the function box.

Following the above steps, you can add more functions. You can enter the operators and fields by choosing the “Insert Operator” and “Insert Field” buttons.

The formula for the validation rule is given below:

NOT(REGEX(Industry_Code__c, "^[ABCD]\\d{6}$"))

The detailed explanation of the function is as follows:

  • REGEX Function: The REGEX function checks whether a text field matches a specified pattern using regular expressions.
  • Industry_Code__c: It specifies the field used to check for validation.
  • ^[ABCD]\\d{6}$: It is the regular expression pattern that you need to match. It has the following characters which are as follow:
    • ^: Specify the start of the string.
    • [ABCD]: Used to match any character from the set of characters A, B, C, or D.
    • \\d{6}: It matches the exactly six digits from 0 to 9.
    • $: It specifies the end of the string.
  • NOT Function: The NOT function checks if the Industry Code does not match the specified pattern defined in the REGEX function; it returns TRUE, otherwise FALSE.
See also  FIND() Function in Salesforce

Click on the “Check Syntax” button to check if the formula is correct or has any errors. If there is no error, you will get the message “No errors found”.

Salesforce Lightning REGEX Function Example

6. Enter an appropriate error message that will be displayed when the validation rule criteria are met. According to your scenario, you can add the error message as given below:

Industry code must start with A, B, C, or D followed by six digits.

In the “Error Location” section, you can choose where the error message appears.

  • If you select “Field”, the error message will display directly within the field.
  • Choosing “Top of Page” displays the error at the page’s top.

7. Click the “Save” button to save the validation rule.

REGEX Function in Salesforce Lightning Example

8. Let’s test the validation rule by editing the existing record in the “Account” object.

Enter the “Industry Code” that doesn’t follow the specified format of the regression expression. For example, I add the industry code as “P45#895”.

Hence, the industry code is not as per the standard defined in the regex function. The validation rule gets activated and triggers an error, as shown below.

Example of Salesforce REGEX Function

By following the above steps, you have learned to create a validation rule in Salesforce Lightning that checks whether the industry code field contains one of the predefined options followed by six digits or not.

How to create the Validation Rule using the REGEX function in Salesforce Classic

Let’s take a scenario to understand how to use the REGEX function in Salesforce Classic in the validation rule.

Scenario: Suppose you have a custom field “Company Size” on the “Lead” object to categories the clients based on the company size. You need to create the validation rule that makes sure that the entered company size matches one of the predefined options.

1. Click the “Setup” option to navigate to the Salesforce Classic setup page. If you don’t see the option there, click on your username at the top right corner of the page and then select “Setup” from the dropdown menu.

Salesforce Classic REGEX Function

2. Move downward to the “Build” section, expand the “Customize” dropdown, and select the standard object on which you want to apply the validation rule.

As per the scenario, select the “Leads” object.

3. Once you expand the object, find and select the “Validation Rules” option.

Salesforce REGEX Function Example

4. To create a new validation rule, click on the “New” button on the right.

Salesforce Classic REGEX Function Example

5. Fill up the details for the validation rule.

  • Rule Name: Enter the name for the validation rule.
  • Description: Enter the description to give more detail about the validation rule.
  • Active: If you want to make the validation rule activated, check the “Active” checkbox.

In accordance with the scenario, you can enter the “Company Size Validation” as the rule name.

REGEX Function in Salesforce Classic

6. Define the formula that defines when the validation rule should trigger. You create a validation rule that the company size code field contains one of the predefined options S, M, L, or Xl for small, medium, large, and extra-large companies.

To create the formula for the validation rule, navigate to “All Function Categories”. From there, choose the “Function Category”, followed by a selection of the desired “Function”. Once you are done with the selection, click on “Insert Selected Function”.

See also  FLOOR() Function in Salesforce | Calculate round off nearest integer in Salesforce

After adding the function, replace the parameters with the appropriate fields, functions, and operations according to your validation rule. The following is the formula for the validation rule:

NOT(REGEX(Company_Size__c, "^(S|M|L|XL)$"))

A detailed description of the formula is given below:

  • REGEX Function: The REGEX Function is used to check if the text string matches a specified pattern.
  • Company Size: It is the field that you want to evaluate.
  • Regular Expression: The pattern is given as follows:
    • ^: The symbol specifies the start of the string.
    • (S|M|L|XL): It specifies the group of characters that the field value matches. The field value must have one of the characters S, M, L, or XL.
    • $: The symbol denotes the end of the string.
  • NOT Function: The NOT function negates the result of the expression inside it. If the expression inside the function returns TRUE, the NOT function converts it to FALSE.

To ensure the accuracy of the formula, click on the “Check Syntax” button. If there are any errors, an error message will be displayed.

REGEX Function in Salesforce Classic Example

7. Enter the message you want to display when the validation rule is triggered to let the user know about the error.

Please enter a valid company size code. The options are 'S' for Small, 'M' for Medium, 'L' for Large, or 'XL' for Extra Large.

Select the desired location where you want to display the error message. You can choose between “Top of Page” or “Field” by clicking on the respective radio button.

Note: If your validation rule checks multiple fields, it is suggested to display the error message at the top of the page. It provides clear instructions that there are multiple errors.

8. Once done with the validation rule, click on the “Save” button to save your process of the validation rule.

Example of Salesforce Classic REGEX Function

9. Verify your validation rule after saving it. To do so, create or modify a record in the “Leads” object, and on saving, the record validation rule will be triggered if there is any error.

Let’s suppose the user entered the company size as “XXL” and tried to save the record. But every time the user clicks on “Save”, they get an error as the validation rule gets activated.

The result of the validation rule is given below:

Example of REGEX Function in Salesforce

You have successfully learned to create a validation rule in Salesforce Classic for an object with the use and working of the REGEX function in Salesforce.


In summary, you have learned that the REGEX function matches the regular expression with the text in Salesforce. You can use this function to maintain data entry in Salesforce.

In this tutorial, I have covered the concept of the REGEX function with its syntax, consideration, and example.

Moreover, I have explained step-by-step how to use the REGEX function in Salesforce Lightning and Salesforce Classic within the validation rule.

You may like to read the following articles: