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 YESYESnono
Greedy quantifier \? Makes the preceding item optional. Greedy, so the optional item is included in the match if possible. abc\? matches abc or ab nonononononononononononononononononononononono
Lazy quantifier ?? Makes the preceding item optional. Lazy, so the optional item is excluded in the match if possible. abc?? matches ab or abc YESYESnonononono
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 YESnononononononononononononono
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 YESYES
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 YESYESnonononono
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 YESnononononononononononononono
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 YESYESnono
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 nonononononononononononononononononononononono
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 YESYESnonononono
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 YESnononononononononononononono
Fixed quantifier {n} where n is an integer >= 1 Repeats the previous item exactly n times. a{3} matches aaa YESYESnono
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 YESYESnono
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 YESYESnono
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 V2nononononononononononononononononononono
Fixed quantifier \{n\} where n is an integer >= 1 Repeats the previous item exactly n times. a\{3\} matches aaa nononononononononononononononononononono
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 nononononononononononononononononononono
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 nononononononononononononononononononono
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 nonononononononononononononononononononononono
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 YESYESnonononono
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 YESYESnonononono
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 V2nonononononononononononononononononononono
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 YESnonononononononononononononono
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 YESnonononononononononononononono
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