Digits:
\d+
: Matches one or more digits.\d{4}
: Matches exactly four digits.Non-Digits:
\D+
: Matches one or more non-digits.Word Characters:
\w+
: Matches one or more word characters (alphanumeric characters plus underscore).[a-zA-Z0-9_]+
: Equivalent to \w+
.Non-Word Characters:
\W+
: Matches one or more non-word characters.Whitespace Characters:
\s+
: Matches one or more whitespace characters (space, tab, newline).\S+
: Matches one or more non-whitespace characters.Line Anchors:
^
: Matches the start of a line.$
: Matches the end of a line.Character Sets:
[aeiou]
: Matches any single character in the set (vowels in this case).[^aeiou]
: Matches any single character not in the set (consonants in this case).[a-z]
: Matches any single lowercase alphabetical character.Repetitions:
*
: Matches zero or more occurrences of the preceding element.+
: Matches one or more occurrences of the preceding element.?
: Matches zero or one occurrence of the preceding element.{n}
: Matches exactly n
occurrences of the preceding element.{n,}
: Matches n
or more occurrences of the preceding element.{n,m}
: Matches between n
and m
occurrences of the preceding element.Alternation:
|
: Matches either the pattern before or after the alternation operator.Grouping:
( )
: Groups multiple tokens together.Escape Characters:
\
: Escapes special characters so they can be treated as literal characters.Quantifiers:
?
: Matches zero or one occurrences.*
: Matches zero or more occurrences.+
: Matches one or more occurrences.{n}
: Matches exactly n
occurrences.{n,}
: Matches at least n
occurrences.{n,m}
: Matches between n
and m
occurrences.Anchors:
^
: Matches the start of a line.$
: Matches the end of a line.\b
: Matches a word boundary.\B
: Matches a non-word boundary.Character Classes:
[abc]
: Matches any single character in the set (a, b, or c).[^abc]
: Matches any single character not in the set (any character except a, b, or c).[a-z]
: Matches any single lowercase alphabetical character.[A-Z]
: Matches any single uppercase alphabetical character.[0-9]
: Matches any single digit.Character Escapes:
\.
: Matches a literal dot.\^
: Matches a literal caret.\$
: Matches a literal dollar sign.\(
: Matches a literal opening parenthesis.\)
: Matches a literal closing parenthesis.Quantifiers with Lazy Matching:
*?
: Matches zero or more occurrences, but as few as possible.+?
: Matches one or more occurrences, but as few as possible.??
: Matches zero or one occurrence, but as few as possible.{n,m}?
: Matches between n
and m
occurrences, but as few as possible.Boundary Matchers:
\b
: Matches a word boundary.\B
: Matches a non-word boundary.\A
: Matches the start of the input.\Z
: Matches the end of the input, before any newline characters.\z
: Matches the end of the input.Lookahead and Lookbehind Assertions:
(?=...)
: Positive lookahead assertion.(?!...)
: Negative lookahead assertion.(?<=...)
: Positive lookbehind assertion.(?<!...)
: Negative lookbehind assertion.Named Groups:
(?P<name>...)
: Defines a named capturing group.(?P=name)
: Matches the same text as the most recent capture group named 'name'.Unicode Character Properties:
\p{...}
: Matches any character with the specified Unicode property.\P{...}
: Matches any character without the specified Unicode property.Word Boundaries:
\b
: Matches a word boundary.\B
: Matches a non-word boundary.Matching Specific Quantities:
\d{2,4}
: Matches 2 to 4 digits.\w{3,}
: Matches 3 or more word characters.\s{1,3}
: Matches 1 to 3 whitespace characters.Matching Specific Characters:
[aeiou]{2}
: Matches any two consecutive vowels.[0-9a-fA-F]{2}
: Matches any two hexadecimal digits.Using Capture Groups:
(\d{3})-(\d{3})-(\d{4})
: Matches and captures groups for area code, exchange code, and subscriber number in a phone number.Backreferences:
\1
, \2
, etc.: Matches the same text as the first, second, etc., capturing group.Matching Any Character:
.
: Matches any single character except newline characters.[\s\S]
: Matches any character including newline characters.Conditional Matching:
(?(condition)then|else)
: Matches 'then' if the condition is true, otherwise matches 'else'.