23. Metacharacters - Quantifiers: Intervals
- Finally, the last of the quantifiers is the interval, or range.
- Intervals are constructed using a:
{min, max}
structure, where min is the minimum number of times the qualified character
can match, and max is the maximum. If that character is found a number of
times anywhere in that interval, then the match will succeed.
- This gives us a way of making our search for phone numbers much more
precise than before. Remember that we were using:
[0-9]*[ -]*[0-9]*
- Now we can modify this to be:
[0-9]{4}[ -]*[0-9]{4}
- Notice that the interval can also be used like an "exact number of times"
requirement - in this case, we want to match any digit, then another digit,
etc. 4 times, then zero or more spaces or dashes, and then a digit, another
digit, etc, again, 4 times. This is much more like what we are looking for!
|