Filter Rows Based on Criteria List with Wildcards in Google Sheets

Published on

We can use a criteria list for filtering a table based on multiple conditions in Google Sheets. But what if this list contains wildcards (? and *)?

To handle such scenarios, we can employ the QUERY function in Google Sheets to filter data based on the criteria list with wildcards.

For this example, let’s consider a table containing fruit names with grades in column A and their availability in column B.

The data range is precisely A1:B19, where A1:B1 contains the field labels ‘Fruit Names’ and ‘Status,’ respectively. The criteria list with wildcards is in C2:C4, comprising *Grade C*, *Fuji, and *Nav??, respectively.

Sample data table and criteria list with wildcards

Our goal is to filter the rows based on this criteria list that contains wildcard characters in Google Sheets.

Specifically, we aim to filter items (fruit names) that do not contain Grade C, do not end with Fuji, and do not end with Nav followed by any two characters.

Filter Rows Based on Criteria List with Wildcards: Step-by-Step Instructions

Here are three steps involved in this process:

  1. Substituting Wildcard Characters

For this type of filtering in Google Sheets, we will use the MATCHES (preg) regular expression match in the QUERY function.

To begin, we need to substitute the wildcard character asterisk (*) with (.*), and the question mark (?) with (.).

  • .: Matches any single character except for a new line.
  • *: Matches 0 or more occurrences of the preceding character (in this case, the dot).

Our criteria list with wildcards is in C2:C4. In D2, insert the following nested SUBSTITUTE formula to make these changes.

=ArrayFormula(SUBSTITUTE(SUBSTITUTE(TOCOL(C2:C4, 1), "*", ".*"), "?", "."))

The TOCOL function here is used to eliminate any empty cells in the criteria range.

Please see the output of this substitution in the screenshot below.

Substituting Wildcards with (preg) Regular Expression Wildcards
  1. Creating a Regular Expression Pattern

To perform multiple criteria filtering in Google Sheets with filter criteria in a range and also containing wildcards, we have replaced the wildcards with supporting regular expression wildcards.

Now, combine those criteria separated (delimited) by a pipe.

="'"&TEXTJOIN("|", TRUE, D2:D4)&"'"

This TEXTJOIN function in cell E2 joins the criteria with wildcards in D2:D4 and places a pipe delimiter in between. If any cell in this range is blank, the function will omit that cell.

Output:'.*Grade C.*|.*Fuji|.*Nav..'

  1. Filter Rows Based on the Criteria List with Wildcards

The following QUERY formula filters rows where column A in the range doesn’t match the criteria.

=QUERY(A2:B19, "SELECT * WHERE NOT A MATCHES "&E2)
Example of Filtering Rows Based on Criteria List with Wildcards

If you want to filter rows matching the criteria, use the one below.

=QUERY(A2:B19, "SELECT * WHERE A MATCHES "&E2)

The first formula uses the NOT logical operator, whereas the second one doesn’t.

How to Escape Wildcards in the Data Range to Filter

Sometimes, your rows to filter (records in the table) may contain wildcard characters that you need to treat literally, such as a question mark at the end of a string.

Usually, we use the tilde in such cases. For example, to match Available? we will use Available~?.

However, in a MATCHES regular expression match, the tilde won’t work. Instead, you should use the backslash.

So, when you filter rows based on a criteria list with wildcards, do not include such criteria in the list (here in C2:C4). Instead, place them below the list in D2:D4. This is because we don’t want to perform the substitution of wildcards with MATCHES supported wildcards.

For example, if you want to match any text that ends with Apple?, enter .*Apple? in D5. Accordingly, the range in the formula in E2 should be modified from D2:D4 to D2:D5.

Resources

Above, we utilized the MATCHES function in QUERY to filter rows in a table based on a criteria list with wildcards in Google Sheets. Here are guides on performing complex string comparison operators in Google Sheets, aside from using MATCHES.

  1. How to Use LIKE String Operator in Google Sheets Query
  2. CONTAINS Substring Match in Google Sheets Query for Partial Match
  3. Starts with and Not Starts with Prefix Match in Query
  4. Ends with and Not Ends with Suffix Match in Query
  5. Multiple CONTAINS in WHERE Clause in Google Sheets Query
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.

Sort Data but Keep Blank Rows in Excel and Google Sheets

Using a dynamic array formula or the FILTER command, we can sort data and...

Lookup Values Under Categories in Excel and Google Sheets

We can use a combination of XLOOKUP and VLOOKUP in both Excel and Google...

Extract Items Under a Category from the Same Column in Excel

In Excel, you can use a combination of the XLOOKUP and DROP functions to...

How to Create a Searchable Table in Excel Using the FILTER Function

Finding specific records, or rows containing the required information, is straightforward in Excel using...

More like this

XMATCH Row by Row: Finding Values Across a Range in Google Sheets

Using the BYROW function with XMATCH in Google Sheets allows us to match values...

Limit Formula Expansion to a Specific Row in Google Sheets

In this tutorial, I’ll explain how to limit the expansion of an array formula...

3-D Referencing Structured Data Tables in Google Sheets

When you have several tables within a single sheet—not across multiple sheets in a...

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.