Emacs: Regex Tutorial

By Xah Lee. Date: . Last updated: .

Regular Expression (or regex in short) is a character sequence that represent a pattern for matching text. For example, you can use it to find all email addresses in a file by matching the email address pattern. Regex is used by many functions to check if a string contains certain pattern, or extract it, or replace it with other string.

xah talk show 2022-08-04 emacs regular expression

What is Wrong with Wildcards * ?

Regex Commands

The most commonly used commands that use regex are:


  1. Open a file with many lines.
  2. Alt+x list-matching-lines
  3. type th.t

It will list all lines that contains the regex pattern th.t, which includes β€œthis”, β€œthat”. The dot . character in regex is a wildcard, matching any character.

Emacs Regular Expression Syntax

Interactive Emacs Regex Mode

Emacs has a interactive regex mode. It show matches as you type. To go into the mode, Alt+x regexp-builder. (I don't like this)

I recommend using Alt+x list-matching-lines to test your pattern.

Emacs Find Replace

Emacs Regular Expression

Regex in Emacs Lisp

ErgoEmacs mascot-s276x226
Buy Xah Emacs Tutorial

Emacs Tutorial

Quick Start


Split Window



Find Replace




Rectangle Edit

Line Wrap


View Special File

Editing Brackets

Org Mode


Emacs Efficiency