Guide to Common Regular Expression Syntax and Patterns
Quantifiers
? (Optional)
Matches the preceding element zero or one time.
colou?r
----------
color /* matches */
colour /* matches */
colouur /* does not match */
* (Zero or More)
Matches the preceding element zero, one, or multiple times.
go*gle
----------
gle /* matches */
gogle /* matches */
googgle /* matches */
goagle /* does not match */
go /* does not match */
+ (One or More)
Matches the preceding element one or more times.
go+gle
----------
gle /* does not match */
gogle /* matches */
googgle /* matches */
goagle /* does not match */
go /* does not match */
{} (Specific Counts)
Specifies the exact number or range of repetiitons for the preceding element.
lo{3}ves /* 'o' must appear exactly 3 times */
lo{2,5}ves /* 'o' must appear between 2 and 5 times */
lo{2,}ves /* 'o' must appear 2 or more times */
() (Groupign)
Groups multiple characters together to be treated as a single unit.
(lo)\1 /* Matches 'lolo' */
--------------
lovely /* does not match */
lolo /* matches */
Logical OR
| (Alternation)
Acts as a boolean OR, matching the expression before or after the pipe.
(apple|orange) juice
----------
apple juice /* matches */
orange juice /* matches */
grape juice /* does not match */
Character Classes
- (Range)
Defines a range of characters inside square brackets [].
[0-9a-fA-F]+ /* Matches hexadecimal characters */
^ (Negation in Classes)
When used inside [], it negates the set, matching any characetr not listed.
[^a-zA-Z]+ /* Matches characters that are NOT letters */
Metacharacters
\d and \D
\d matches any digit (equivalent to [0-9]). \D matches any non-digit.
\d{3}-\d{4} /* Matches a 7-digit number pattern like 123-4567 */
\w and \W
\w matches any word character (alphanumeric and underscore). \W matches any non-word character.
\w+ /* Matches identifiers like 'variable_1' */
\s and \S
\s matches any whitespace (space, tab, newline). \S matches any non-whitespace.
\s+ /* Matches gaps between words */
. (Dot)
Matches any single character except for a newline character.
h.t /* Matches 'hat', 'hot', 'h6t', etc. */
^ (Start of Line)
Asserts the position at the start of a line.
^Hello /* Matches 'Hello' only if it is at the start of the line */
----------
Hello World /* matches */
World Hello /* does not match */
$ (End of Line)
Asserts the position at the end of a line.
World$ /* Matches 'World' only if it is at the end of the line */
----------
Hello World /* matches */
World Hello /* does not match */