How to control the overal mode of a regular expression.
Repetition
This page describes how to repeat the preceeding token match.
N.B. - When experimenting with greediness and laziness, note that although the repeater is associted with (and repeats) the token on it's left, it is terminated by the token on it's right. The token on the right of the repeater is by no means compulsory, but it's absence makes the difference between greedy and lazy negligable. On consideration, this knowledge will yield the practical usefulness of the greedy/lazy distinction.
Character
Description
Example
Ways to specify common repeats
?
"question mark"
Specifies an optional token.
abc?
matches
ab
or
abc
*
"greedy star"
Repeats the token zero or more times. This repeat is greedy, it will attempt to match the greatest possible number of repeats in the current token.
".*"
matches
"def" "ghi"
in
abc "def" "ghi" jkl
*?
"lazy star"
Repeats the token zero or more times. This repeat is lazy, it will attempt to match the fewest possible number of repeats in the current token.
".*?"
matches
"def"
in
abc "def" "ghi" jkl
+
"greedy plus"
Repeats the token one or more times. This repeat is greedy, it will attempt to match the greatest possible number of repeats of the current token.
".+"
matches
"def" "ghi"
in
abc "def" "ghi" jkl
+?
"lazy plus"
Repeats the token one or more times. This repeat is lazy, it will attempt to match the fewest possible number of repeats in the current token.
".+?"
matches
"def"
in
abc "def" "ghi" jkl
Ways to specify custom repeats
{n}
where n is an integer >= 1
Repeats the token exactly n times.
a{3}
matches
aaa
{n,m}
where n >= 0 and m >= n "greedy"
Repeats the token between n and m times. This repeat is greedy, it will attempt to match the current token as many times as possible; up to m times.
a{2,4}
matches
aaaa
,
aaa
or
aa
{n,m}?
where n >= 0 and m >= n "lazy"
Repeats the token between n and m times. This repeat is lazy, it will attempt to match the current token as few times as possible; down to n times.
a{2,4}?
matches
aa
,
aaa
or
aaaa
{n,}
where n >= 0
Repeats the token at least n times. This repeat is greedy, it will attempt to match the current token as many times as possible.
a{2,}
matches
aaaaa
in
aaaaa
{n,}?
where n >= 0
Repeats the token at least n times. This repeat is lazy, it will attempt to match the current token as few times as possible; down to n times.