Regular Expression Syntax
The simplest kind of regular expression is a literal string. More complicated patterns involve the use of metacharacters to describe all the different choices and variations that you want to build into a pattern. Metacharacters don’t match themselves, but describe something else. The metacharacters are:
Metacharacter | Meaning |
|---|---|
| Escapes the character(s) immediately following it |
| Matches any single character except a newline
(unless |
| Matches at the beginning of the string (or
line, if |
| Matches at the end of the string (or line, if
|
| Matches the preceding element 0 or more times |
| Matches the preceding element 1 or more times |
| Matches the preceding element 0 or 1 times |
| Specifies a range of occurrences for the element preceding it |
| Matches any one of the class of characters contained within the brackets |
| Groups regular expressions |
| Matches either the expression preceding or following it |
The . (single dot)
is a wildcard character. When used in a regular expression, it can
match any single character. The exception is the newline character
(\n), except when you use the
/s modifier on the pattern match
operator. This modifier treats the string to be matched against as a
single “long” string with embedded newlines.
The ^ and $ metacharacters are used as anchors in a
regular expression. The ^ matches the beginning of a line. This character should appear only at the beginning of an expression to match ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access