Regular expression closures

These match a series of characters by matching the pattern they follow zero or more time.

Character class Meaning
P* Matches the pattern P zero or more times
P+ Matches the pattern P one or more times
P? Matches the pattern P zero or one times
P{n} Matches the pattern P exactly n times
P{n,} Matches the pattern P at least n times
P{n,m} Matches the pattern P at least n but not more than m times.

All closure operators (*, +, ?, {n,m}) are greedy by default, meaning that they match as many characters of the string as possible without causing the overall match to fail.

A reluctant closure matches as few characters of the string as possible without causing the overall match to fail. To specify a reluctant closure, append a ? to the closure pattern. Valid patterns are P*?, P+?, and P??.

Related Links