- 论坛徽章:
- 0
|
When used at the end of a regular expression, requires that
the preceding regular expression be found at the end of the
line; for example, here matches only when here: occurs at
the end of a line. When not at the end of a regular expression,
$ stands for itself.
\
Treats the following special character as an ordinary
character. For example, \. matches an actual period instead
of "any single character," and \* matches an actual asterisk
instead of "any number of a character." The \ (backslash)
prevents the interpretation of a special character. This
prevention is called "escaping the character." (Use \\ to get a
literal backslash.)
[ ]
Matches any one of the characters enclosed between the
brackets. For example, [AB] matches either A or B, and
p[aeiou]t matches pat, pet, pit, pot, or put. A range of
consecutive characters can be specified by separating the first
and last characters in the range with a hyphen. For example,
[A-Z] will match any uppercase letter from A to Z, and [0-9]
will match any digit from 0 to 9.
You can include more than one range inside brackets, and you
can specify a mix of ranges and separate characters. For
example, [:;A-Za-z()] will match four different punctuation
marks, plus all letters.
Most metacharacters lose their special meaning inside
brackets, so you don't need to escape them if you want to use
them as ordinary characters. Within brackets, the three
metacharacters you still need to escape are \-]. The hyphen
(-) acquires meaning as a range specifier; to use an actual
hyphen, you can also place it as the first character inside the
brackets.
A caret (^) has special meaning only when it is the first
character inside the brackets, but in this case the meaning
differs from that of the normal ^ metacharacter. As the first
character within brackets, a ^ reverses their sense: the
brackets will match any one character not in the list. For
example, [^a-z] matches any character that is not a
lowercase letter. |
|