Excel Function: REGEXTEST (2/3)

Character Quantity

Quantifiers allow you to specify how many times a character can appear. To specify a quantity or a range of quantities, add {} after the concerned character.


For example:

The regex "Composer{0,}" checks here if the text contains this word with none, one, or multiple "r":

=REGEXTEST(A2,"Composer{0,}")

excel function regextest character class quantity 2

There are also 3 handy shortcuts for quantifying a character:

The regex "Composer*" is therefore identical to the previous one:

=REGEXTEST(A2,"Composer*")

excel function regextest character class quantifier 2

Testing a Structure

To test the structure of a text (for example a reference, a URL, a phone number, etc.), you will need to use several elements seen so far in a single expression.

For example, to validate the regex "^XLP-[0-9]{4}-" the text must:

=REGEXTEST(A2,"^XLP-[0-9]{4}-")

excel function regextest ref 2

Special Characters

Metacharacters are special characters that have a specific role (you already know ^ $ [ ] | ? * + { }).

To cancel the effect of a metacharacter and treat it as a simple character, precede it with a \ (except when the metacharacter is inside a character class).

Thanks to \, the regex " \?$" can check if the text ends with " ?":

=REGEXTEST(A2," \?$")

excel function regextest backslash 2

The metacharacters are \ ^ $ . [ ] | ( ) ? * + { }.
Also note that \t equals a tab, \r a carriage return, and \n a line break.

Character Class Shortcuts

As you now know, character classes define allowed or disallowed characters. There are some handy shortcuts to simplify this:

The regex "^[A-Z]{2}.\w{4}$" checks if the text starts with 2 characters from A-Z, followed by any character (except line break), followed by (and ending with) 4 alphanumeric characters:

=REGEXTEST(A2,"^[A-Z]{2}.\w{4}$")

excel function regextest shortcuts class 2

Case-Insensitive Search

To search for the word "excel" without considering case sensitivity, add (?i) at the beginning of the regex "(?i)excel":

=REGEXTEST(A2,"(?i)excel")

excel function regextest case insensitive 2

Or, even simpler, you can directly enter the value 1 as the third argument of the REGEXTEST function:

=REGEXTEST(A2,"excel",1)

excel function regextest argument case insensitive 2

Email Address Validation

A regex to validate an email address (simplified version) could look like "^[a-z0-9_.-]+@[a-z0-9_.-]{2,}\.[a-z]{2,}$".

To validate this regex, the text must:

=REGEXTEST(A2,"^[a-z0-9_.-]+@[a-z0-9_.-]{2,}\.[a-z]{2,}$")

excel function regextest email 2

The shortcut \w is not used here instead of [a-zA-Z0-9_] because \w also includes accented characters, which is not desirable here.