Quick Start
Tutorial
Tools & Languages
Examples
Reference
Book Reviews
Regex Reference
Introduction
Table of Contents
Quick Reference
Characters
Basic Features
Character Classes
Shorthands
Anchors
Word Boundaries
Quantifiers
Unicode
Capturing Groups & Backreferences
Named Groups & Backreferences
Special Groups
Mode Modifiers
Recursion & Balancing Groups
Replacement Reference
Characters
Matched Text & Backreferences
Context & Case Conversion
Conditionals
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference
Book Reviews
Printable PDF
About This Site
RSS Feed & Blog
RegexBuddy—Better than a regular expression reference!

Regular Expression Reference: Quantifiers

FeatureSyntaxDescriptionExampleJGsoft .NET Java Perl PCRE PCRE2 PHP Delphi R JavaScript VBScript XRegExp Python Ruby std::regex Boost Tcl ARE POSIX BRE POSIX ERE GNU BRE GNU ERE Oracle XML XPath
Greedy quantifier ? (question mark) Makes the preceding item optional. Greedy, so the optional item is included in the match if possible. abc? matches abc or ab YESYESYESYESYESYESYESYESYESYESYESYESYESYESECMA
extended
egrep
awk
ECMA
extended
egrep
awk
YESnoYESnoYESYESYESYES
Greedy quantifier \? Makes the preceding item optional. Greedy, so the optional item is included in the match if possible. abc\? matches abc or ab nononononononononononononononononononoYESnononono
Lazy quantifier ?? Makes the preceding item optional. Lazy, so the optional item is excluded in the match if possible. abc?? matches ab or abc YESYESYESYESYESYESYESYESYESYESYESYESYESYESECMAECMAYESnononono10gR2noYES
Possessive quantifier ?+ Makes the preceding item optional. Possessive, so if the optional item can be matched, then the quantifier won’t give up its match even if the remainder of the regex fails. abc?+c matches abcc but not abc YESnoYES5.10YESYESYESYESYESnononono1.9noECMA
1.42–1.85
nononononononono
Greedy quantifier * (star) Repeats the previous item zero or more times. Greedy, so as many items as possible will be matched before trying permutations with less matches of the preceding item, up to the point where the preceding item is not matched at all. ".*" matches "def" "ghi" in abc "def" "ghi" jkl YESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYES
Lazy quantifier *? Repeats the previous item zero or more times. Lazy, so the engine first attempts to skip the previous item, before trying permutations with ever increasing matches of the preceding item. ".*?" matches "def" and "ghi" in abc "def" "ghi" jkl YESYESYESYESYESYESYESYESYESYESYESYESYESYESECMAECMAYESnononono10gR2noYES
Possessive quantifier *+ Repeats the previous item zero or more times. Possessive, so as many items as possible will be matched, without trying any permutations with less matches even if the remainder of the regex fails. ".*+" can never match anything YESnoYES5.10YESYESYESYESYESnononono1.9noECMA
1.42–1.85
nononononononono
Greedy quantifier + (plus) Repeats the previous item once or more. Greedy, so as many items as possible will be matched before trying permutations with less matches of the preceding item, up to the point where the preceding item is matched only once. ".+" matches "def" "ghi" in abc "def" "ghi" jkl YESYESYESYESYESYESYESYESYESYESYESYESYESYESECMA
extended
egrep
awk
ECMA
extended
egrep
awk
YESnoYESnoYESYESYESYES
Greedy quantifier \+ Repeats the previous item once or more. Greedy, so as many items as possible will be matched before trying permutations with less matches of the preceding item, up to the point where the preceding item is matched only once. ".\+" matches "def" "ghi" in abc "def" "ghi" jkl nononononononononononononononononononoYESnononono
Lazy quantifier +? Repeats the previous item once or more. Lazy, so the engine first matches the previous item only once, before trying permutations with ever increasing matches of the preceding item. ".+?" matches "def" and "ghi" in abc "def" "ghi" jkl YESYESYESYESYESYESYESYESYESYESYESYESYESYESECMAECMAYESnononono10gR2noYES
Possessive quantifier ++ Repeats the previous item once or more. Possessive, so as many items as possible will be matched, without trying any permutations with less matches even if the remainder of the regex fails. ".++" can never match anything YESnoYES5.10YESYESYESYESYESnononono1.9noECMA
1.42–1.85
nononononononono
Fixed quantifier {n} where n is an integer >= 1 Repeats the previous item exactly n times. a{3} matches aaa YESYESYESYESYESYESYESYESYESYESYESYESYESYESECMA
extended
egrep
awk
ECMA
extended
egrep
awk
YESnoYESnoYESYESYESYES
Greedy quantifier {n,m} where n >= 0 and m >= n Repeats the previous item between n and m times. Greedy, so repeating m times is tried before reducing the repetition to n times. a{2,4} matches aaaa, aaa or aa YESYESYESYESYESYESYESYESYESYESYESYESYESYESECMA
extended
egrep
awk
ECMA
extended
egrep
awk
YESnoYESnoYESYESYESYES
Greedy quantifier {n,} where n >= 0 Repeats the previous item at least n times. Greedy, so as many items as possible will be matched before trying permutations with less matches of the preceding item, up to the point where the preceding item is matched only n times. a{2,} matches aaaaa in aaaaa YESYESYESYESYESYESYESYESYESYESYESYESYESYESECMA
extended
egrep
awk
ECMA
extended
egrep
awk
YESnoYESnoYESYESYESYES
Greedy quantifier {,m} where m >= 1 Repeats the previous item between zero and m times. Greedy, so repeating m times is tried before reducing the repetition to zero times. a{,4} matches aaaa, aaa, aa, a, or the empty string V2nononononononononononoYES1.9nonononononoYESnonono
Fixed quantifier \{n\} where n is an integer >= 1 Repeats the previous item exactly n times. a\{3\} matches aaa nonononononononononononononobasic
grep
basic
grep
noYESnoYESnononono
Greedy quantifier \{n,m\} where n >= 0 and m >= n Repeats the previous item between n and m times. Greedy, so repeating m times is tried before reducing the repetition to n times. a\{2,4\} matches aaaa, aaa or aa nonononononononononononononobasic
grep
basic
grep
noYESnoYESnononono
Greedy quantifier \{n,\} where n >= 0 Repeats the previous item at least n times. Greedy, so as many items as possible will be matched before trying permutations with less matches of the preceding item, up to the point where the preceding item is matched only n times. a\{2,\} matches aaaaa in aaaaa nonononononononononononononobasic
grep
basic
grep
noYESnoYESnononono
Greedy quantifier \{,m\} where m >= 1 Repeats the previous item between zero and m times. Greedy, so repeating m times is tried before reducing the repetition to zero times. a\{,4\} matches aaaa, aaa, aa, a, or the empty string nononononononononononononononononononoYESnononono
Lazy quantifier {n,m}? where n >= 0 and m >= n Repeats the previous item between n and m times. Lazy, so repeating n times is tried before increasing the repetition to m times. a{2,4}? matches aa, aaa or aaaa YESYESYESYESYESYESYESYESYESYESYESYESYESYESECMAECMAYESnononono10gR2noYES
Lazy quantifier {n,}? where n >= 0 Repeats the previous item n or more times. Lazy, so the engine first matches the previous item n times, before trying permutations with ever increasing matches of the preceding item. a{2,}? matches aa in aaaaa YESYESYESYESYESYESYESYESYESYESYESYESYESYESECMAECMAYESnononono10gR2noYES
Lazy quantifier {,m}? where m >= 1 Repeats the previous item between zero and m times. Lazy, so repeating zero times is tried before increasing the repetition to m times. a{,4}? matches the empty string, a, aa, aaa or aaaa V2nononononononononononoYES1.9nononononononononono
Possessive quantifier {n,m}+ where n >= 0 and m >= n Repeats the previous item between n and m times. Possessive, so as many items as possible up to m will be matched, without trying any permutations with less matches even if the remainder of the regex fails. a{2,4}+a matches aaaaa but not aaaa YESnoYES5.10YESYESYESYESYESnonononononoECMA
1.42–1.85
nononononononono
Possessive quantifier {n,}+ where n >= 0 Repeats the previous item n or more times. Possessive, so as many items as possible will be matched, without trying any permutations with less matches even if the remainder of the regex fails. a{2,}+a never matches anything YESnoYES5.10YESYESYESYESYESnonononononoECMA
1.42–1.85
nononononononono
FeatureSyntaxDescriptionExampleJGsoft .NET Java Perl PCRE PCRE2 PHP Delphi R JavaScript VBScript XRegExp Python Ruby std::regex Boost Tcl ARE POSIX BRE POSIX ERE GNU BRE GNU ERE Oracle XML XPath