Contents

  Icon

Quantifiers

Anchors

Basic metacharacters

Character classes

Comments

Control and code

Grouping

Look around

Mode modifiers

Quantifiers

Specific characters

Normally, each item in an expression will only match once. Quantifiers allow you to match the previous character or group a number of times. The ordinary quantifiers are greedy, meaning they will match as often as possible. If you add a question mark (?) quantifiers become lazy, and will match as few times as possible.

bullet Greedy quantifiers

bullet Lazy quantifiers

Greedy quantifiers

 

Item

 

Match:

 
 

?

 

1 or 0 times.
Equivalent of {0,1}.

 
 

*

 

As many times as possible.
Equivalent of {0,}.

 
 

+

 

As many times as possible, but at least 1 time.
Equivalent of {1,}.

 
 

{Number}

 

Exactly Number times.

 
 

{Min,}

 

As many times as possible, but at least Min times.

 
 

{Min,Max}

 

At most Max times, and at least Min times.

 
 

Lazy quantifiers

 

Item

 

Match:

 
 

??

 

0 or 1 times.
Equivalent of {0,1}?.

 
 

*?

 

As few times as possible.
Equivalent of {0,?}.

 
 

+?

 

As few times as possible, but at least 1 time.
Equivalent of {1,?}.

 
 

{Number}?

 

Exactly Number times.
Equivalent of {Number}.

 
 

{Min,}?

 

As few times as possible, but at least Min times.

 
 

{Min,Max}?

 

At least Min times, but at most Max times.