Skip to content

regex

Descriptive explanations of commonly used regex patterns.

Matching Basics

Core patterns to match common text elements.

Match any single character (except newline)

A pattern that matches any single character except for line breaks.

\.
Matches any character before the newline. "a\nb".match(.) only matches "a"

Match the start of a string

Anchors the pattern to the beginning of a string.

^Hello
Matches "Hello" only if it's at the start of the line.

Match the end of a string

Anchors the pattern to the end of a string.

world$
Matches "world" only if it's at the end of the line.

Match any digit

Matches any numeric digit (0-9).

\d
Matches any single digit like 0, 1, 2, ..., 9.

Match any non-digit

Matches any character that is not a digit.

\D
Matches any character that is not a number, such as A, b, $, etc.

Match any whitespace character

Matches any whitespace character (space, tab, newline, etc.).

\s
Matches a space, tab, or newline character.

Match any non-whitespace character

Matches any character that is not whitespace.

\S
Matches any character that is not a space, tab, or newline.

Match a word character

Matches letters (a-z, A-Z), numbers (0-9), and underscore (_).

\w
Matches any character a-z, A-Z, 0-9, and _.

Match a non-word character

Matches any character that is not a word character.

\W
Matches any character that is not a-z, A-Z, 0-9, or _.

Match any newline

Matches a newline character.

\n
Matches the line feed character (ASCII 10, often used to indicate the end of a line).

Character Sets & Ranges

Patterns to specify a set of allowable characters or ranges.

Match a specific character set

Matches any character within the specified set of characters.

\[aeiou\]
Matches any vowel.

Match a range of characters

Matches characters within the specified range.

\[a-z\]
Matches any lowercase letter.

Match any character except specified ones

Matches characters not in the specified set.

\[\^aeiou\]
Matches any character that is not a vowel.

Match a character from a set or range

Matches any character that matches any of the patterns.

\[a-zA-Z0-9_\]
Matches any alphanumeric or underscore.

Quantifiers

Specify how many times a pattern must occur.

Match zero or more times

Matches the preceding pattern zero or more times.

\*
Matches zero or more occurrences of the preceding character or group.

Match one or more times

Matches the preceding pattern one or more times.

+
Matches one or more occurrences of the preceding character or group.

Match zero or one time

Matches the preceding pattern zero or one time.

\?
Matches zero or one occurrence of the preceding character or group.

Match a specific number of times

Matches the preceding pattern an exact number of times.

\{3\}
Matches exactly 3 occurrences of the preceding character or group.

Match a range of times

Matches the preceding pattern within a range.

\{2,5\}
Matches between 2 and 5 occurrences (inclusive) of the preceding character or group.

Match at least n times

Matches the preceding pattern at least n times.

\{2,\}
Matches 2 or more occurrences of the preceding character or group.

Groups and Lookaheads

Advanced patterns for grouping and conditional matches.

Group patterns together

Groups multiple patterns to treat them as a single unit.

(abc)
Matches the sequence "abc".

Positive lookahead

Matches a group if it is followed by another pattern (but doesn't consume it).

(?=abc)
Matches if the next characters are "abc", but the match pointer stays where it was.

Negative lookahead

Matches a group if it is not followed by another pattern (but doesn't consume it).

(?!abc)
Matches if the next characters are NOT "abc".

Capture groups

Captures matched text for reference or replacement.

(group)
Captures the matched "group" in a numbered group.

Non-capturing group

Groups patterns without capturing the matched text.

(?:abc)
Groups "abc" but doesn't capture it.

Named capture group

Captures matched text with a name.

(?<name>group)
Captures the matched "group" with the name "name".

Positive lookbehind

Matches a group if it is preceded by another pattern (but doesn't consume it).

(?<=abc)
Matches if the preceding characters are "abc".

Negative lookbehind

Matches a group if it is not preceded by another pattern (but doesn't consume it).

(?<!abc)
Matches if the preceding characters are NOT "abc".

Escaping Special Characters

Handling characters that have special meanings in regex.

Escape a special character

Matches the literal character instead of its special meaning.

\\

Match a literal dot

Matches the literal '.' character.

\.

Match a literal asterisk

Matches the literal '*' character.

\*

Match a literal plus sign

Matches the literal '+' character.

\+