Java Reference
In-Depth Information
Special Characters Used in Regular Expressions (continued)
Character
Examples
Function
^
/^A/ matches the fi rst but
not the second A in “ A man
called Adam
Matches the start of a line or of the input .
$
/r$/ matches only the last
r in “ horror
Matches the end of a line or of the input .
*
/ro*/ matches r in
right ,“ ro in “ wrong ,“
and “ roo “ in “ room
Matches the preceding character zero or more times.
+
/l+/ matches l in “life,“
ll in “still,“ and lll in
“stilllife“
Matches the preceding character one or more times.
For example, /a+/ matches the a in “candy“ and all
the a's i n “caaaaaaandy.”
?
/Smythe?/ matches
“Smyth” and “Smythe”
Matches the preceding character once or zero times.
.
/.b/ matches the second
but not the fi rst ob in
“blob“
Matches any character apart from the newline
character.
(x)
/(Smythe?)/ matches
“Smyth“ and “Smythe“ in
“John Smyth and Rob
Smythe“ and allows the
substrings to be retrieved
as RegExp.$1 and
RegExp.$2 respectively.
Matches x and remembers the match. The matched
substring can be retrieved from the elements of the
array that results from the match, or from the RegExp
object's properties $1, $2 ... $9, or lastParen.
x|y
/Smith|Smythe/ matches
“Smith” and “Smythe”
Matches either x or y (where x and y are blocks of
characters).
{n}
/l{2}/ matches ll in
still “ and the fi rst two
l s in “ stilllife
Matches exactly n instances of the preceding charac-
ter (where n is a positive integer).
{n,}
/l{2,}/ matches ll
in “ still ” and lll in
stilllife
Matches n or more instances of the preceding charac-
ter (where n is a positive integer).
{n,m}
/l{1,2}/ matches l in
life “, ll in “ still ,“
and the fi rst two l s in
stilllife
Matches between n and m instances of the preceding
character (where n and m are positive integers).
[xyz]
[ab] matches a and b ;
[a-c] matches a , b and c
Matches any one of the characters in the square
brackets. A range of characters in the alphabet can be
matched using a hyphen.
Search WWH ::




Custom Search