What is REGEX:

REGEX stands for a regular expression. This function is useful to guide the user in creating data in a proper format into the system.
To maintain quality data in the system it will be used in validation rules.

Expressions to use in REGEX:

  • . (period) – It will match any character except a line terminator
  • ? (question mark) – It will match the preceding token 0 or 1 times
  • * (asterisk) – It will match the preceding token 0 or more times
  • + (plus sign) – It will match the preceding token 1 or more times
  • {n} – It will match the preceding token n times
  • {n,} – It will match the preceding token n or more times
  • {0,} is equivalent to * and {1,} is equivalent to +
  • {n,m} – It will match the preceding token at least n times and no more than m times
  • ?? – To define once or not at all
  • *? – To define zero or more times
  • +? – To define one or more times
  • {n}? – To define exactly n times
  • {n,}? – To define at least n times
  • {n,m}? – To define at least n times and up to m times
  • {n,m}+ – To define at least n times and up to m times
  • \ (backslash) – Combine with another character to mean something else
  • | (pipe or vertical bar) – To define OR operator
  • - (dash or hyphen) – For indicating a sequence (range) of characters
  • () (parentheses) – enclose character groups
  • [ (open bracket) – enclose character classes
  • { (open curly brace) – Use when multiplying a token a specified number of times

Character-class operators:

  • Literal escape \x
  • Grouping […]
  • Range a-z
  • Union [a-e][i-u]
  • Intersection [a-z&&[AEIOU]]

Backslash – Character Combinations:

Some characters combined with a backslash to define certain non-alphanumeric characters:

  • \\t – To define tab character
  • \\n – To define a new line (linefeed) character (this is a line terminator character)
  • \\r – To define carriage return character (this is a line terminator character)
  • \\f – It is a form feed character
  • \\a – Alert (bell) character
  • \\e – Escape character
  • \\cx – The control character corresponding to "x" (ctrl-B would be "\\cB")

Predefined Characters:

  • \\d – It matches any digit (shorthand for "[0-9]")
  • \\D – It matches any non-digit (shorthand for "[^0-9]")
  • \\s – To define a white space character (space, tab, new line, form feed, carriage return, and \\x0B)
  • \\w – To define a "word character" (shorthand for "[a-zA-z_0-9]")
  • \\W – To define a non-word character (shorthand for "[^a-zA-z_0-9]" or "[^\w]")

Boundaries:

  • ^ – To define the beginning of a line
  • $ – To define the end of a line
  • \\b – A word boundary
  • \\B – A non-word boundary
  • \\A – To define the beginning of an input
  • \\G – To define the end of the previous match
  • \\Z – To define the end of the input except for a final terminator
  • \\z – To define the end of the input

Example: 1
Social Security Number format:

The length of the SSN Number is 9. Out of 9 digits, the first 3 digits should be between 0 to 9 and then the next two digits are between 0 to 5 and the last 4 digits are between 0 to 2. If the field is not blank and the user enters a number other than this format it will throw validation.

Validation Rule:

NOT(REGEX( SocialSecurityNumber__c , "[0-9]{3}-[0-5]{2}-[0-2]{4}"))

Example-2
State Code Format:

The length of the State Code is 5. Out of 9, the first 2 should be in between 0 to 9 and then the next three should be in between a to z. If the user enters a number other than this format it will throw validation.

Validation Rule:

NOT( REGEX(StateCode_c, "[a-z]{2}-[0-9]{3})")

Example-3
Credit Card Number Format:

The Credit Card Number field accepts 16 digits of the numbers 0-9 with or without a dash. If the entered number by the user is not in this format it will throw validation. If we use a dash(-), group the 16 digits in four digits per dash(-).

Validation Rule:

NOT( REGEX( CreditCardNumber__c , "(((\\d{4}-){3}\\d{4})|\\d{16})?"))

Example-4
Phone Number Format:

If user enters a phone number other than below format it will throw validation.

Validation Rule:

NOT(REGEX(Phone, "([1-9]{2})-[1-9]{10}"))

Example-5

Description is a long text area field that accepts multiple lines of text. If we would like to restrict the user not to use line break then use the below validation rule.

Validation Rule:

REGEX(Description, '(.*\r?\n.*)*')

Example-6

Description is a long text area field that accepts multiple lines of text. If we would like to restrict the user not to enter more than 4 lines, then use the below validation rule. Refer this blog to learn: How to implement Custom Discounts in Salesforce CPQ

Validation Rule:

REGEX(Description, '(.*\r?\n.*){4,}')

About the Author

profile image

Srilakshmi

Associate Analyst (CRM)

Srilakshmi is working with Jade Global as an Associate Analyst in CRM. She has 4+ years of extensive experience on Implementation projects in Sales Cloud, Nintex DocGen, CPQ. She is fervent to acquaint herself with the new things. She works with the business directly to develop solutions and provides support to current Salesforce.com for various clients of Jade Global Inc.

How Can We Help You?