Table of Contents
Quick look at Excel regular expression functions: REGEXTEST, REGEXEXTRACT and REGEXREPLACE
Home Software Tutorial Office Software How to Use the REGEX Functions in Excel

How to Use the REGEX Functions in Excel

Feb 06, 2025 pm 10:28 PM

Quick look at Excel regular expression functions: REGEXTEST, REGEXEXTRACT and REGEXREPLACE

Regular expression (REGEX) is a search pattern used to check if a text string complies with a given pattern and to extract or replace a text string matching the given pattern. Given its complexity, this article provides a simplified summary and example of its use in Excel.

The

REGEX function is suitable for users who use Microsoft 365 Excel for Windows or Mac, as well as those who use Excel on the web.

REGEXTEST: Text Pattern Matching Test

This function tests whether the text string matches the given pattern and returns TRUE or FALSE based on the test results. This is a great way to test whether the data follows a specific pattern.

Grammar

<code>REGEXTEST(a, b, c)</code>
Copy after login
Copy after login
Copy after login

Of:

  • a (Required) is the text, value, or cell reference to the text to be tested.
  • b (Required) is the mode used to perform tests.
  • c (Optional) 0 if you want the test to be case sensitive; otherwise 1.

REGEXTEST Example Use

This spreadsheet contains a list of product codes that must follow a strict structure.

How to Use the REGEX Functions in Excel

Factory codes include:

  • Lower case representation of product size ("xs" means very small, "s" means small, "m" means medium, etc.),
  • Represents one or two digits of product material,
  • Three capital letters representing the location of the product manufacturing, and
  • The three parts are separated by short horizontal lines.

We need to test whether all product codes match this structure.

So, in cell B2, enter the following formula:

<code>=REGEXTEST([@Code],"[xs|s|m|l|xl]-[0-9]{1,2}-[A-Z]{3}",0)</code>
Copy after login
Copy after login
Copy after login

Of:

  • [@Code] is a structured reference to the column containing the code to be tested.
  • [xs|s|m|l|xl] is the first part of the product code to be tested, and the vertical line indicates "or".
  • [0-9]{1,2} is the second part of the product code to be tested, [0-9] means any one digit, and {1,2} means there can be one or two digits.
  • [A-Z]{3} is the third part of the product code to be tested, [A-Z] means any capital letters, and {3} means there must be exactly three capital letters.
  • The three parts of the code to be tested are separated by short horizontal lines.
  • 0 is the last parameter in the formula, which tells Excel to test case sensitivity.

After pressing Enter to apply this formula to all rows in column B, the result shows that only two codes are valid (TRUE).

How to Use the REGEX Functions in Excel

  • m-2-UK is invalid (indicated by FALSE results) because the country code contains only two capital letters.
  • xl-714-AUS is invalid because the material code contains three digits.
  • S-5-USA is invalid because the size code is capitalized.

This example contains the use of characters such as [] and {}. However, there are many other characters (also called markers) that can also be used to determine the pattern used to perform the test, some of which will be used in the examples below.

REGEXEXTRACT: Extract specific text fragments

This function returns part of the text in the cell according to the specified pattern. For example, you might want to separate numbers and text.

Grammar

<code>REGEXTEST(a, b, c)</code>
Copy after login
Copy after login
Copy after login

Of:

  • d (Required) is the text, value, or cell reference from which the text is to be extracted.
  • e (Required) is the pattern to be extracted.
  • f (Optional) 0 if you want to extract only the first match; 1 if you want to extract all applicable matches as an array; Match extract group, then 2.
  • g (Optional) 0 if you want the extraction to be case sensitive; otherwise 1.

Because the formatted Excel table cannot handle overflow arrays, if you plan to extract the match as an array in the parameter f, make sure your data is in normal format.

REGEXEXTRACT Example Use

In this example, the customer's name and phone number need to be extracted into three separate columns.

How to Use the REGEX Functions in Excel

First focus on the name. In cell B2, enter the following formula:

<code>=REGEXTEST([@Code],"[xs|s|m|l|xl]-[0-9]{1,2}-[A-Z]{3}",0)</code>
Copy after login
Copy after login
Copy after login

Of:

  • A2 is the cell containing the data to be extracted.
  • [A-Z][a-z] Tell Excel to extract any word that starts with uppercase letters followed by lowercase letters, where " " means to return one or more lowercase letters in each pattern.
  • 1 means you want to separate each example of the above pattern into individual cells as an array (in other words, the first name is in cell B2 and the second name is in cell C2). If this parameter is omitted, Excel returns only the first match (first name) in cell B2.

After pressing Enter, Excel performs the extraction successfully and adds a light blue line around cell C2 to remind you that it is an overflow array.

How to Use the REGEX Functions in Excel

After selecting cell B2, you can now copy this relative formula to the remaining details rows using the fill handle in the lower right corner of the cell.

How to Use the REGEX Functions in Excel

Now, a similar REGEXTRACT formula needs to be used to extract the customer's phone number. In cell D2, enter the following formula:

<code>REGEXEXTRACT(d, e, f, g)</code>
Copy after login

Of:

  • A2 is the cell containing the data to be extracted.
  • [0-9()] The numbers from zero to nine extract the numbers in parentheses, where " " extracts one or more numbers in this pattern.
  • [0-9-] Extract the remaining numbers in the string. The second "-" represents a short horizontal line that separates the two parts of the phone number. " " tells Excel that if the string contains numbers, one or more of them are to be extracted. number.

Since there is only one instance of this pattern in each cell in column A, no additional parameters need to be added. Likewise, once you check that this formula produces the expected result, you can use the fill handle to copy it to the remaining cells in column D.

How to Use the REGEX Functions in Excel

There are other ways in Excel that extract data and get similar results, such as using the TEXTSPLIT function or Excel's Quick Fill tool.

REGEXREPLACE: Operational data

This function takes the text in a cell and creates a new version of the data in another cell. Even if the function is called REGEXREPLACE, it does not actually replace the original text in its original position.

Grammar

<code>REGEXTEST(a, b, c)</code>
Copy after login
Copy after login
Copy after login

Of:

  • h (Required) is the text, value, or cell reference to the text to be replaced.
  • i (Required) is the mode to be replaced.
  • j (Required) is the replacement content to be created.
  • k (Optional) is the number of occurrences of the pattern to be replaced.
  • l (Optional) 0 if you want the replacement to be case sensitive; otherwise 1.

REGEXREPLACE Example of usage

Below, you can see a series of names in column A. The goal is to recreate these names in column B, but use the "last name, first" format, including commas that separate names.

How to Use the REGEX Functions in Excel

In cell B2, enter the following formula:

<code>=REGEXTEST([@Code],"[xs|s|m|l|xl]-[0-9]{1,2}-[A-Z]{3}",0)</code>
Copy after login
Copy after login
Copy after login

Of:

  • [@Client name] Refers to the column containing the data to affect.
  • [A-Z][a-z] Include twice in the formula (and separated by spaces), tell Excel to get two text strings containing uppercase letters followed by one or more lowercase letters.
  • , Tell Excel to reverse the order of these two text strings and separate them with commas and spaces. If the dollar sign is not included, Excel will only return "2, 1" as the result for each cell.

The parameters k and l> are not processed in the above formula, because you want Excel to replace all occurrences (the default values ​​of parameter k) and you want to replace Case sensitive (default value for parameter l).

Because the formatted table is used, the formula will be applied to the remaining cells in column B after pressing Enter.

How to Use the REGEX Functions in Excel


Regular expressions are not only used in Excel. In fact, you can use REGEX to automatically perform other tasks on your computer, such as fixing copy-paste PDF text, batch renaming downloaded files, formatting currency, removing HTML tags, and more.

The above is the detailed content of How to Use the REGEX Functions in Excel. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to Create a Timeline Filter in Excel How to Create a Timeline Filter in Excel Apr 03, 2025 am 03:51 AM

In Excel, using the timeline filter can display data by time period more efficiently, which is more convenient than using the filter button. The Timeline is a dynamic filtering option that allows you to quickly display data for a single date, month, quarter, or year. Step 1: Convert data to pivot table First, convert the original Excel data into a pivot table. Select any cell in the data table (formatted or not) and click PivotTable on the Insert tab of the ribbon. Related: How to Create Pivot Tables in Microsoft Excel Don't be intimidated by the pivot table! We will teach you basic skills that you can master in minutes. Related Articles In the dialog box, make sure the entire data range is selected (

You Need to Know What the Hash Sign Does in Excel Formulas You Need to Know What the Hash Sign Does in Excel Formulas Apr 08, 2025 am 12:55 AM

Excel Overflow Range Operator (#) enables formulas to be automatically adjusted to accommodate changes in overflow range size. This feature is only available for Microsoft 365 Excel for Windows or Mac. Common functions such as UNIQUE, COUNTIF, and SORTBY can be used in conjunction with overflow range operators to generate dynamic sortable lists. The pound sign (#) in the Excel formula is also called the overflow range operator, which instructs the program to consider all results in the overflow range. Therefore, even if the overflow range increases or decreases, the formula containing # will automatically reflect this change. How to list and sort unique values ​​in Microsoft Excel

If You Don't Rename Tables in Excel, Today's the Day to Start If You Don't Rename Tables in Excel, Today's the Day to Start Apr 15, 2025 am 12:58 AM

Quick link Why should tables be named in Excel How to name a table in Excel Excel table naming rules and techniques By default, tables in Excel are named Table1, Table2, Table3, and so on. However, you don't have to stick to these tags. In fact, it would be better if you don't! In this quick guide, I will explain why you should always rename tables in Excel and show you how to do this. Why should tables be named in Excel While it may take some time to develop the habit of naming tables in Excel (if you don't usually do this), the following reasons illustrate today

How to Format a Spilled Array in Excel How to Format a Spilled Array in Excel Apr 10, 2025 pm 12:01 PM

Use formula conditional formatting to handle overflow arrays in Excel Direct formatting of overflow arrays in Excel can cause problems, especially when the data shape or size changes. Formula-based conditional formatting rules allow automatic formatting to be adjusted when data parameters change. Adding a dollar sign ($) before a column reference applies a rule to all rows in the data. In Excel, you can apply direct formatting to the values ​​or background of a cell to make the spreadsheet easier to read. However, when an Excel formula returns a set of values ​​(called overflow arrays), applying direct formatting will cause problems if the size or shape of the data changes. Suppose you have this spreadsheet with overflow results from the PIVOTBY formula,

How to Use the PIVOTBY Function in Excel How to Use the PIVOTBY Function in Excel Apr 11, 2025 am 12:56 AM

Quick Links The PIVOTBY Syntax

Excel MATCH function with formula examples Excel MATCH function with formula examples Apr 15, 2025 am 11:21 AM

This tutorial explains how to use MATCH function in Excel with formula examples. It also shows how to improve your lookup formulas by a making dynamic formula with VLOOKUP and MATCH. In Microsoft Excel, there are many different lookup/ref

How to Use Excel's AGGREGATE Function to Refine Calculations How to Use Excel's AGGREGATE Function to Refine Calculations Apr 12, 2025 am 12:54 AM

Quick Links The AGGREGATE Syntax

How to change Excel table styles and remove table formatting How to change Excel table styles and remove table formatting Apr 19, 2025 am 11:45 AM

This tutorial shows you how to quickly apply, modify, and remove Excel table styles while preserving all table functionalities. Want to make your Excel tables look exactly how you want? Read on! After creating an Excel table, the first step is usual

See all articles