A regular expression is a pattern. Some parts of the pattern match single characters in the string of a particular type. Other parts of the pattern match multiple characters. First, we’ll visit the single-character patterns, and then the multiple-character patterns.
The simplest and most common
pattern-matching character in regular expressions is a single
character that matches itself. In other words, putting a letter
a in a regular expression requires a corresponding
a in the string.
The next most common
pattern-matching character is the dot
.“. This character matches any single
example, the pattern
/a./ matches any two-letter
sequence that starts with
a and is not
A pattern-matching character class is represented by a pair of open and close square brackets and a list of characters between the brackets. One and only one of these characters must be present at the corresponding part of the string for the pattern to match. For example,
matches a string containing any one of the first five letters of the lowercase alphabet, while
matches any of the five vowels in either lower- or uppercase.
If you want to put a right bracket (
]) in the
list, put a backslash in front of it, or put it as the first
character within the list.
Ranges of characters (like
z) can be abbreviated
by showing the end points of the range separated by a
-); to get a literal dash in the list, precede ...