DavidSzczesniak
12/19/2017 - 4:26 PM

Regex Quantifiers

These are special characters that provide more flexibility, changing the number of times each character can match.

\!h # There are three main quantifiers:
- ? matching 0 or 1 occurence 
- + matching 1 or more occurence 
- * matching 0 or more (i.e. any amount)

# In addition to that, using curly braces we can have even more flexible quantifiers

\!h # Example: color vs colour - optional characters
# the long way
if ($str =~ /color/ or $str =~ /colour/) {
  ...
}
# faster way with a quantifier
if ($str =~ /colou?r/) {
  ...
}
# Here, the ? says the letter 'u' can appear 0 or 1 times.
# Therefore, either 'color' or 'colour' will work.

\!h # The curly braces:
# These can be used to express alot of differnt amounts.
# Normally, they are used to express a range:
x{2, 4} would mean 2, 3 or 4 x-es
# removing the upper limit:
x{2,} would mean 2 or more x-es
# removing the comma:
x{2} would mean exactly 2 x-es

\!h # Quantifiers on character classes
# As well as individual characters, we can also use quantifiers on special characters or character classes
/[0-9]+/ meaning 1 or more digits
/[0-9]{2-4}/ meaning 2 to 4 digits
/-[abc]+-/ meaning 1 or more occurences of a, b or c between two dashes
# as a comparison, the character class /-[abc]-/ (without the quantifier), will match:
-a- 
-b- 
# but not:
-aa-
-ab-
--
-x-
# with the quantifier it will match:
-a-
-b-
-aa-
-ab-
# but not:
--
-x-
# with an * in place of the + it will match an empty field -- but still not a different character -x-