Emacs: Regular Expression
Regular Expression (aka regex) 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.
(video tutorial: xah talk show 2022-08-04 emacs regular expression)
Regex Commands
The most commonly used commands that use regex are:
- Alt+x
list-matching-lines
[see Emacs: List/Delete Matching Lines] - Alt+x
query-replace-regexp
[see Emacs: Find Replace in Current File]
Example:
- Open a file with many lines.
- Alt+x
list-matching-lines
- 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
- Regular Expression
- Regex Syntax
- About Quoting Regex
- Case Sensitivity
- How to Insert a Tab or Newline
- Wildcards vs Regex
- Emacs Regex vs Python, JavaScript, Java