What is a Regular Expression?

Metacharacters are unique characters that possess specific meanings and play a vital role in defining patterns within regular expressions:

CharacterDescriptionExample
[]A collection of characters, representing a range or a set of allowed characters"[Ll]iberty"
\Indicates a special sequence or is used to escape special characters"\d{4}"
.Any character, excluding newline, often used as a wildcard"La.s"
^Specifies that the pattern must begin with the given characters"^Regex"
$Specifies that the pattern must end with the given characters"toolbuilder$"
*Allows zero or more occurrences of the preceding character or group"Re*gex"
+Allows one or more occurrences of the preceding character or group"La+bs"
?Allows zero or one occurrences of the preceding character or group"Libert?y"
{}Precisely the stated number of occurrences of the preceding character or group"La{2}s"
|Provides an alternative option between two patterns"Liberty|Labs"
( )Used to capture and group a portion of the pattern-

Special Sequences

A special sequence consists of a \ followed by one of the characters listed below, giving it a unique meaning within the regular expression:

CharacterDescriptionExample
\AMatches if the specified characters are at the start of the string"\ALiberty"
\bMatches if the specified characters are at the beginning or end of a word (use "r" in the beginning for treating the string as a "raw string")r"\bLabs", r"Regex\b"
\BMatches if the specified characters are in the string, but NOT at the beginning or end of a word (use "r" in the beginning for treating the string as a "raw string")r"\BR", r"L\B"
\dMatches if the string contains digits (numbers 0-9)"Lab\d"
\DMatches if the string DOES NOT contain digits"Regex\D"
\sMatches if the string contains a white space character"Liberty\sLabs"
\SMatches if the string DOES NOT contain a white space character"toolbuilder\S"
\wMatches if the string contains any word characters (letters a to Z, digits 0-9, and the underscore _ character)"Liberty\w"
\WMatches if the string DOES NOT contain any word characters"Labs\W"
\ZMatches if the specified characters are at the end of the string"toolbuilder\Z"