To match strings in Google Sheets, use the EXACT function for case-sensitive matches, or the = operator for case-insensitive matches. For partial matches or pattern-based matching, use REGEXMATCH or COUNTIF with wildcards. This article explains all the methods, when to use each, and how to handle common pitfalls.
Why String Matching Matters in Sheets
String matching is the process of comparing text values to find identical, similar, or pattern-based matches. It’s a daily need for anyone cleaning data, checking duplicates, categorizing entries, or building lookup formulas. Google Sheets offers several functions that handle string comparison, each with different behavior regarding case sensitivity, wildcards, and regular expressions. Choosing the right one depends on whether you need an exact match, a partial match, or a match against a pattern.
The Core Functions: EXACT, =, and REGEXMATCH
The simplest way to compare two strings is with the equals sign: =A1=B1. This returns TRUE if the strings are identical, ignoring case. For example, "apple" and "APPLE" would be considered equal. If you need case-sensitive matching, use EXACT: =EXACT(A1, B1) returns TRUE only if the text is exactly the same, including capitalization. For partial matches, REGEXMATCH is powerful: =REGEXMATCH(A1, "needle") returns TRUE if the text in A1 contains the substring “needle”. Regular expressions allow complex patterns, like "^[A-Z]{3}" to match exactly three uppercase letters at the start.
Matching with Wildcards in COUNTIF and SUMIF
When you need to count or sum cells that match a pattern, COUNTIF and SUMIF support wildcards: asterisk (*) matches any sequence of characters, and question mark (?) matches any single character. For instance, =COUNTIF(A1:A10, "*apple*") counts cells containing the word “apple”. To match a literal asterisk, you must escape it with a tilde (~). These functions are not case-sensitive, so “Apple” and “apple” both match the pattern. This is often the fastest way to check if any cell in a range matches a substring without writing an array formula.
Using QUERY for Advanced String Matching
Google Sheets’ QUERY function can also filter rows based on string conditions using SQL-like syntax. For example, =QUERY(A1:B10, "SELECT * WHERE A CONTAINS 'apple'") returns all rows where column A contains “apple”. You can use STARTS WITH, ENDS WITH, and MATCHES (for regular expressions). Note that string literals in QUERY are enclosed in single quotes, and you must be careful with apostrophes in the data (double them up). QUERY is case-insensitive by default, but you can use LOWER() or UPPER() to normalize case.
Partial vs. Exact Match: Which One Do You Need?
Deciding between exact and partial matching depends on your goal. If you’re looking up a value in a table with VLOOKUP or INDEX/MATCH, you usually want an exact match (set the last argument to FALSE). Partial matches can lead to incorrect results if multiple rows contain the same substring. For data validation or deduplication, you often need exact matches to avoid false positives. Use EXACT when case matters (like product codes). For searching user input, partial matches are more forgiving. Always test your data for edge cases like leading/trailing spaces, which can break exact matches—consider using TRIM first.
Common Problems and Solutions
Several issues frequently arise when matching strings. Case sensitivity: The = operator is case-insensitive; if you need case-sensitive, use EXACT. Wildcards in COUNTIF: The tilde (~) escapes wildcards, so =COUNTIF(A1:A10, "~*") counts cells containing a literal asterisk. Regular expressions: REGEXMATCH uses RE2 syntax; some features like lookahead are not supported. Spaces: Invisible spaces can cause mismatches; use =TRIM(A1) to clean data. Locale differences: Some locales use semicolons as argument separators; check your spreadsheet settings. Array formulas: To compare a range against a value, you may need ARRAYFORMULA, e.g., =ARRAYFORMULA(SUM(--(A1:A10="apple"))) to count exact matches.
Comparing with Lookup Functions
String matching is often used in lookups. VLOOKUP with exact match: =VLOOKUP("apple", A1:B10, 2, FALSE). INDEX/MATCH is more flexible: =INDEX(B1:B10, MATCH("apple", A1:A10, 0)). For partial matches in lookups, you can use wildcards with VLOOKUP: =VLOOKUP("*apple*", A1:B10, 2, FALSE) returns the first match. However, wildcard lookups are slow on large datasets; consider using FILTER or QUERY instead. Note that XLOOKUP is not available in Google Sheets—use INDEX/MATCH or FILTER.
Case-Insensitive Matching and Lower/UPPER
If you need to ignore case but also need to use EXACT (which is case-sensitive), you can normalize both strings with LOWER or UPPER. For example, =EXACT(LOWER(A1), LOWER(B1)) performs a case-insensitive exact match. This is useful when you want to treat “New York” and “new york” as the same. Similarly, REGEXMATCH is case-sensitive by default, but you can use (?i) at the start of the pattern to make it case-insensitive: =REGEXMATCH(A1, "(?i)apple"). This is a handy trick for pattern matching without changing the data.
Frequently Asked Questions
How do I check if a cell contains a specific word? Use =REGEXMATCH(A1, "word") for a simple substring check, or =COUNTIF(A1, "*word*")>0 for a non-array formula. Both are case-insensitive by default; add (?i) to REGEXMATCH if needed.
Why does VLOOKUP return #N/A even though the text looks the same? This is often due to trailing spaces or non-printing characters. Use =TRIM(A1) and =CLEAN(A1) to remove spaces and line breaks. Also check that the lookup value is not a number formatted as text.
Can I match strings with wildcards in REGEXMATCH? REGEXMATCH uses regular expressions, not wildcards. Use .* instead of *, and . instead of ?. For example, .*apple.* matches any string containing “apple”.
Is there a way to compare two columns and highlight matches? Use conditional formatting with a custom formula. Select the range, go to Format > Conditional formatting, choose “Custom formula is”, and enter =EXACT(A1, B1) (or =A1=B1 for case-insensitive) to highlight cells where the row matches.
How do I count how many cells in a column contain a specific substring? Use =COUNTIF(A1:A100, "*substring*"). This counts cells that contain the substring, regardless of case. For case-sensitive counting, use =SUMPRODUCT(--ISNUMBER(FIND("substring", A1:A100))).
What’s the difference between MATCH and REGEXMATCH? MATCH returns the position of a value in a range (used for lookups), while REGEXMATCH returns TRUE/FALSE if a text matches a regular expression. They serve different purposes: MATCH for finding a value, REGEXMATCH for testing a pattern.
Related
- Balance Sheet Item 5: What It Is and How to Read It
- Google Sheets BIN2HEX Function Explained
- Google Sheets Split Expenses Template: How to Use
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- SUMIF Negative Values in Google Sheets: A Clear GuideTo add negative numbers in Google Sheets, use SUMIF with the criterion "<0". If the values are in…
- How to Create a Header Template in WordTo create a reusable header template in Word, design the header in a document, then save the file…
- How to Write Dates in Google SheetsTo write a date in Google Sheets, select a cell, enter a date in a format your spreadsheet’s…
- Tab Switching Shortcuts: Ctrl+Tab, Ctrl+Page Down & MoreThe fastest way to switch between tabs in a web browser is Ctrl+Tab (Windows) or Ctrl+Page Down (Windows)…
- How to Keep a Running Count in ExcelTo keep a running count in Excel, put your records in one column and enter =COUNTA($A$2:A2) beside the…
- Multiple Project Tracker Spreadsheet Setup and TipsA multiple project tracker spreadsheet is a shared workbook that keeps tasks from several projects in one consistent,…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.