Skip to content

Latest commit

 

History

History
68 lines (50 loc) · 1.63 KB

File metadata and controls

68 lines (50 loc) · 1.63 KB

reference_quantifiers.md

Regular Expression Reference

PCRE compared to other flavors

QUANTIFIERS

* - quantifier: some, one or no one

echo "13" | perl -ne 'print if(/12*3/);' # output 13
echo "1222223" | perl -ne 'print if(/12*3/);' # output: 1222223

? - quantifier: one or no one

echo "13" | perl -ne 'print if(/12?3/);' output: 13
echo "123" | perl -ne 'print if(/12?3/);' # output: 123

+ - some, or at less one
x+ means the same as xx*

echo "12223" | perl -ne 'print if(/12+3/);' # output: 12223
echo "123" | perl -ne 'print if(/12+3/);' # output: 123

{ } - quantifier: a number of {exactly} {min,max} {min,}
The min is required, the max allowed

echo "abbc" | perl -ne 'print if(/ab{2}c/);' # output: abbc
echo "abbbbbc" | perl -ne 'print if(/ab{3,}c/);' # output: abbbbbc
echo "abbbbbc" | perl -ne 'print if(/ab{2,4}c/);' # output:

( ) - delimit the value of a quantifier
() are also used for many other reasons such as alternate, backreferences and so on...

echo "abababc" | perl -ne 'print if(/(ab){3}c/);' # output: abababc

*? +? ?? {min,max}? - lazy quantifiers
Quantifiers are normally greedy. Lazy quantifiers prefer a shorter match to a longer one.

echo "-how to-disappear-" | perl -pe 's/-[^-]+-/-X-/;' # output: -X-disappear-

*?+ ++ + {min,max}+ - possessive quantifiers
like greedy quantifiers BUT they don't allow backtracking to their match (increase speed)
only supported in Java & PCRE, not supported in perl 5.8.0 (use atomic grouping instead)