How to Use Regular Expressions (Regex) in Google Docs for Advanced Find and Replace

Introduction

The standard Find and Replace tool in Google Docs is excellent for swapping specific words, but it struggles with complex patterns. For example, finding every email address, phone number, or double space in a 100-page document is impossible with a standard search. Google Docs supports Regular Expressions (Regex), a powerful search syntax that allows you to find and manipulate text based on intricate patterns. This guide explains how to utilize Regex in Google Docs.

Step 1: Enable Regex in Find and Replace

Open your Google Doc. Press Ctrl + H (or Cmd + Shift + H on Mac) to open the Find and Replace dialog box. To activate Regex functionality, you must check the box labeled Match using regular expressions. Note that checking this box will automatically force the “Match case” option to be checked as well.

Step 2: Finding Patterns

Instead of typing a specific word in the “Find” field, you input a Regex pattern. Here are some highly useful patterns for document editing:

  • Find all double spaces: \s{2,} (This searches for two or more consecutive whitespace characters).
  • Find any digit: \d (Finds single numbers 0-9. Use \d+ to find multi-digit numbers).
  • Find a specific word variation: colou?r (The question mark makes the preceding character optional, so this finds both “color” and “colour”).
  • Find email addresses (basic): [\w.-]+@[\w.-]+\.\w+

Step 3: Advanced Replacing with Capture Groups

The true power of Regex is the ability to capture parts of your search and rearrange them in the “Replace with” field. This is done using Capture Groups (parentheses) and backreferences.

Suppose you have a document with names formatted as LastName, FirstName (e.g., “Smith, John”), and you want to flip them to FirstName LastName (“John Smith”).

  1. In the Find box, type: (\w+),\s(\w+)
    • (\w+) captures the last name as Group 1.
    • ,\s matches the comma and space.
    • (\w+) captures the first name as Group 2.
  2. In the Replace with box, type: $2 $1
    • $2 outputs the second capture group (First Name).
    • The space inserts a space.
    • $1 outputs the first capture group (Last Name).

Click Replace All, and every name in the document will instantly flip to the correct format.

Important Limitations

The Regex engine in Google Docs is robust, but it operates slightly differently than standard programming Regex engines. Most notably, Google Docs Regex cannot match patterns across multiple paragraphs (it will not match across hard line breaks or carriage returns). It evaluates text line by line.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.