Ranges, {min, max}
Ranges are considered "counting qualifiers" in regular expressions. This is because they
specify the minimum number of matches to find and the maximum number of matches to allow. Use
ranges in regex searches when a bound, or a limit, should be placed on search results. For
example, the range {3,5}
matches an item at least 3 times, but not more than 5
times. When this range is combined with the regex, a{3,5}
, the strings
"aaa", "aaaa", and "aaaaa" are successfully matched. If only a single number is
expressed within curly braces {3}
, the pattern matches exactly three items. For
example, the regex b{3}
matches the string "bbb".
Using ranges to identify search patterns.
Example 1: Match the preceding "0" at least 3 times with a maximum of 5 times.
- Regex:
60{3,5} years
- Matches:
6000 years 60000 years 600000 years
- Doesn't Match:
60 years 600 years 6003 years 6000000 years
Example 2: Using the "." wildcard to match any character sequence two or three characters long.
- Regex:
.{2,3}
- Matches:
404 44 com w3
- Doesn't Match:
4 a aaaa
Example 3: Match the preceding "e" exactly twice.
- Regex:
be{2}t
- Matches:
beet
- Doesn't Match:
bet beat eee
Example 4: Match the preceding "w" exactly three times.
- Regex:
w{3}\.mydomain\.com
- Matches:
www.mydomain.com
- Doesn't Match:
web.mydomain.com w3.mydomain.com