What is a Regular Expression?
Metacharacters are unique characters that possess specific meanings and play a vital role in defining patterns within regular expressions:
Character | Description | Example |
---|---|---|
[] | 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:
Character | Description | Example |
---|---|---|
\A | Matches if the specified characters are at the start of the string | "\ALiberty" |
\b | Matches 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" |
\B | Matches 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" |
\d | Matches if the string contains digits (numbers 0-9) | "Lab\d" |
\D | Matches if the string DOES NOT contain digits | "Regex\D" |
\s | Matches if the string contains a white space character | "Liberty\sLabs" |
\S | Matches if the string DOES NOT contain a white space character | "toolbuilder\S" |
\w | Matches if the string contains any word characters (letters a to Z, digits 0-9, and the underscore _ character) | "Liberty\w" |
\W | Matches if the string DOES NOT contain any word characters | "Labs\W" |
\Z | Matches if the specified characters are at the end of the string | "toolbuilder\Z" |