XLOOKUP with Match Mode 3 in Excel (Regex Match)

Published on

XLOOKUP becomes more powerful in Excel with the new match mode 3, which enables using regex patterns as the search key.

This is helpful in various scenarios such as case-sensitive matching, partial word matching, specifying multiple search keys to match either of the first occurrences, and more.

In this tutorial, you will find examples of common lookup scenarios that you can achieve using match mode 3 in Excel.

Introduction

Regular expressions in XLOOKUP provide a way to describe sets of character strings for lookup. To use this, you must specify match mode 3, which is the 5th argument in the function. XLOOKUP checks whether a particular string matches the set described by a regular expression in the lookup range and returns the corresponding value from the result range.

Here are some common regex patterns you can use when specifying match mode 3:

  • . – Any character
  • ^ – Beginning of text
  • $ – End of text
  • (?i) – Case-insensitive match
  • [a-z] – Lowercase letters a to z
  • [A-Z] – Uppercase letters A to Z
  • [0-9] – Any numerical digit
  • \b – Word boundary
  • x* – Zero or more occurrences of ‘x’
  • x+ – One or more occurrences of ‘x’
  • x? – Zero or one occurrence of ‘x’
  • x{n,} – ‘n’ or more occurrences of ‘x’
  • [\d] – Digits
  • [^\d] – Non-digits
  • [0-9A-Za-z] – Alphanumeric characters
  • [A-Za-z] – Alphabetic characters
  • x|y – ‘x’ or ‘y’

Excel adheres to the PCRE2 (Perl Compatible Regular Expressions) flavor of regex, giving access to a rich set of regex features, including complex pattern matching, capturing groups, lookarounds, and more. This provides advanced text search functionality in Excel.

To enable regex-based lookup in XLOOKUP, use match mode 3. Here’s the syntax:

=XLOOKUP(lookup_value, lookup_array, return_array, [if_not_found], [match_mode], [search_mode])

Examples of XLOOKUP with Match Mode 3 in Excel

Below are some practical examples of using regex patterns in XLOOKUP with match mode 3.

1. Basic Use Case

=XLOOKUP("apple", A:A, B:B, "Not found!", 3)

This matches the keyword “apple” in column A and returns the corresponding value from column B. The match is case-sensitive and can be exact or partial. If no match is found, the formula returns “Not found!”.

2. Starts With

=XLOOKUP("^Gravel", A:A, B:B, "Not found!", 3)

Matches strings that start with “Gravel” in column A and returns the first matching value from column B. Useful for entries like “Gravel 5-10 mm”, “Gravel 10-20 mm”.

Wildcard alternative (case-insensitive):

=XLOOKUP("Gravel*", A:A, B:B, "Not found!", 2)

Key Difference: Match mode 3 is case-sensitive, while match mode 2 (wildcards) is not.

3. Ends With

=XLOOKUP("10-20 mm$", A:A, B:B, "Not found!", 3)

Matches items ending with “10-20 mm” in column A.

Wildcard alternative:

=XLOOKUP("*10-20 mm", A:A, B:B, "Not found!", 2)

4. Case-Sensitivity

=XLOOKUP("(?i)^Gravel", A:A, B:B, "Not found!", 3)
=XLOOKUP("(?i)10-20 mm$", A:A, B:B, "Not found!", 3)

Adding the (?i) flag makes the regex case-insensitive. If you want to enforce case sensitivity again later in the pattern, you can use (?-i) or simply remove the inline flag.

5. Alphanumeric Match

=XLOOKUP(".*[a-zA-Z].*[0-9].*|.*[0-9].*[a-zA-Z].*", A:A, B:B, "Not found!", 3)

Matches strings containing both letters and numbers, regardless of order.

XLOOKUP with Match Mode 3 for Alphanumeric Search Key Lookup in Excel

6. Multiple Match (This or That)

=XLOOKUP("UK|United Kingdom", A:A, B:B, "Not found!", 3)

Matches either “UK” or “United Kingdom”.

7. Match Multiple Occurrences of a Keyword

=XLOOKUP("(?:.*apple){2}", A:A, B:B, "Not found!", 3)

Finds strings containing “apple” at least twice.

8. Lookup Inside Brackets

=XLOOKUP("\[(.+)\]", A:A, B:B, "Not found!", 3)

Finds values enclosed in square brackets [ ].

9. Word Boundary Lookup

=XLOOKUP("\b(apple)\b", A:A, B:B, "Not found!", 3)

Matches “apple” as a whole word, preventing unintended matches like “pineapple”.

10. Exact Match with Case Sensitivity

For an exact match, we can use a regular formula, but this is case-insensitive:

=XLOOKUP("apple", A:A, B:B, "Not found!", 0)

For case-insensitive exact matches using regex:

=XLOOKUP("(?i)^apple$", A:A, B:B, "Not found!", 3)

To enforce case sensitivity, simply remove (?i). The XLOOKUP function with match mode 3 (regex) is case-sensitive by default. The (?-i) flag is only needed if case insensitivity was enabled earlier in a more complex regex pattern.

Resources

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Convert Google Sheets Calendar into a Table

Most of us use calendar templates in Google Sheets, where we enter data below...

Filter Today’s Events from a Calendar Layout in Google Sheets

Many of us use calendars in Google Sheets to record events. But how do...

Google Sheets Structured Table: Column Total in the First Row

You may encounter a #REF! circular dependency error when attempting to place a column...

Hierarchical Number Sorting in Excel with Modern Functions

A hierarchical numbering system in Excel allows you to organize data into a clear,...

More like this

Hierarchical Number Sorting in Excel with Modern Functions

A hierarchical numbering system in Excel allows you to organize data into a clear,...

Dynamic Formula to Sum Every 7 Rows in Excel

To sum every 7 rows, you can use either a drag-down formula or a...

How to Extract Numbers from Text in Excel with Regex

You can use the REGEXEXTRACT or REGEXREPLACE functions to easily extract numbers from text...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.