What a regex is
A regular expression is a pattern for finding text. cat finds the letters "cat" anywhere, including in "concatenate". Special characters let the pattern describe kinds of text instead of exact letters.
The building blocks
. | any one character except a line break |
\d, \w, \s | a digit; a letter, digit or underscore; a space, tab or line break |
[aeiou], [^0-9] | any one of these characters; any character that is not a digit |
*, +, ? | zero or more, one or more, zero or one of what comes before |
{3}, {2,4} | exactly 3; between 2 and 4 |
^, $ | the start and end of the text (or of each line with the m flag) |
\b | a word boundary: \bcat\b finds "cat" but not "concatenate" |
( ), | | a group you can reuse in a replacement; either this or that |
To match a special character itself, put a backslash in front: \. matches a full stop.
Examples
\d{3}-\d{4}finds numbers written like 555-0123.^[ \t]+|[ \t]+$with the g and m flags finds spaces and tabs at the start and end of each line, ready to be replaced with nothing.(\d{4})-(\d{2})-(\d{2})replaced with$3/$2/$1turns 2024-09-27 into 27/09/2024.
Flags, and greedy matching
The g flag finds every match rather than the first, i ignores case and m makes ^ and $ work per line. Quantifiers are greedy: <.*> matches from the first < to the last > on a line. Add ? to make them stop as early as possible: <.*?>.
Try patterns with live highlighting in the Regex Tester, which uses JavaScript syntax, and use them on your own text in Find and Replace.