FormulaCamp
Completed
5. Advanced Formulas · Modern Functions · 25 min

18. REGEX Functions

Lesson 18 of 20
Task 1 of 5
  1. In C2:C7, check whether the request contains the word "urgent" in any case ("urgent", "URGENT", "Urgent").

Lesson theory

A regular expression (regex) is a pattern that describes what the piece of text you need looks like: "four digits", "something@something.com". Microsoft 365 has three functions for it (they do not exist in Excel 2021 and earlier):

  • REGEXTEST(text, pattern, [case_sensitivity]) returns TRUE if the pattern is found. Case sensitivity: 0 means match case (default), 1 means ignore case;
  • REGEXEXTRACT(text, pattern, [return_mode], [case_sensitivity]) pulls out what it finds. Mode 0 returns the first match, 1 returns all matches (they spill down), 2 returns the groups in parentheses of the first match (they spill to the right);
  • REGEXREPLACE(text, pattern, replacement, [occurrence], [case_sensitivity]) replaces matches. In the replacement, $1, $2 are the contents of the parentheses.

If the pattern is not found, REGEXEXTRACT returns #N/A. Wrap it in IFNA.

The most useful syntax:

  • \d is a digit, \D is a non-digit, \w is a letter, digit or _, \s is a space;
  • [a-z] is one character from a set, [^0-9] is any character except the set;
  • + means one or more, * means zero or more, {4} means exactly four;
  • ^ is the start of the text, $ is the end, \. is a real dot (a plain . is any character);
  • ( ) is a group, | means "or": (LLC|Inc)$.
=REGEXREPLACE(B2,"\D","") keeps only the digits of a phone number.
=REGEXEXTRACT(A2,"ORD-(\d{4})-(\d{4})",2) returns the year and the order number in two neighboring cells.

Excel uses the PCRE2 dialect, the practice grid uses JavaScript regular expressions. The simple patterns in this lesson work the same in both. In Excel you can pass a whole range and the result spills; in the practice grid, pass a single cell and fill the formula down.

Test yourself

1. What does REGEXEXTRACT return if the pattern is not found in the text?
2. Which pattern matches exactly four digits in a row?
3. What does $1 mean in the replacement argument of REGEXREPLACE?
4. Which versions of Excel have the functions REGEXTEST, REGEXEXTRACT and REGEXREPLACE?

Practice is part of Pro

The live spreadsheet, task checking and .xlsx files in all 170 lessons. Your progress is saved in this browser; sync across devices is coming soon.