HomeGoogle DocsSpreadsheetFilter Rows Based on Criteria List with Wildcards in Google Sheets

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.

Creating Custom Descending Sequence Lists in Excel

You can utilize functions like ROW or SEQUENCE in Excel to create a custom...

SUMIFS with XLOOKUP in Excel and Google Sheets

SUMIFS and SUMIF are infamous for producing an "argument must be a range" error...

Counting XLOOKUP Results with COUNTIFS in Excel and Google Sheets

We can use COUNTIF or COUNTIFS alongside an XLOOKUP formula to conditionally count the...

Appointment Schedule Template in Google Sheets

An appointment schedule template in Google Sheets can assist you in efficiently managing your...

More like this

Appointment Schedule Template in Google Sheets

An appointment schedule template in Google Sheets can assist you in efficiently managing your...

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

Interactive Random Task Assigner in Google Sheets

You have multiple tasks and multiple people. Here's a fun way to randomly assign...

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.