|
Search this site
|
Repetition operators in regular expressionsOn the previous page, we mentioned the dot and asterisk combination, meaning "any character repeated any times". The asterisk is what is sometimes called a repetition operator. How repetition operators workRepetition operators are placed after the item to be repeated. We have already seen that .* means any character repeated any number of times. By item, we mean a single character or a character class. So for example, [0-9]* means any number of digits and a* means any number of instances of the letter a. Available repetition operatorsThe following operators all behave in a similar way to the asterisk: they are placed after the repeated item.
Now you can understand why the following expression:
.*[0-9]{10}.*
matches a string containing ten digits. Resolving ambiguity: greedy and reluctant operatorsIn an expression such as that above, there are potential ambiguities. For example, given a string with more than ten characters, how do we decide which .* matches against the extra digits? And what stops the first .* from simply matching against the entire string, digits or not? On the next page we answer these questions by looking at the notion of greedy and reluctant operators. Written by Neil Coffey. Copyright © Javamex UK 2008. All rights reserved. |