From this tutorial I learned about “Regular Expressions – Quantifiers”, and based on this test code used in this tutorial.
Enter your regex: a??
Enter input string to search: a
I found the text "" starting at index 0 and ending at index 0.
I found the text "" starting at index 1 and ending at index 1.
And
Enter your regex: a??
Enter input string to search: aaa
I found the text "" starting at index 0 and ending at index 0.
I found the text "" starting at index 1 and ending at index 1.
I found the text "" starting at index 2 and ending at index 2.
I found the text "" starting at index 3 and ending at index 3.
Also
Enter your regex: a??
Enter input string to search: cab
I found the text "" starting at index 0 and ending at index 0.
I found the text "" starting at index 1 and ending at index 1.
I found the text "" starting at index 2 and ending at index 2.
I found the text "" starting at index 3 and ending at index 3.
Why?
??is a quantifier, so it says how many times the quantified term should be matched, 0 or 1 times, preferably 0.??on its own doesn’t match anything, it just decorates another expression, saying how many times that expression is to be matched in the tested string.Iff the rest of the expression doesn’t match with a lazy
0 timespartial match, it will try again with1 timepartial match.It’s true that if the whole regular expression consists of just a lazy optional match of some term, it will always match empty positions inside the tested string. So this kind of quantifier is only useful when there are other terms around it. For example, the expression
ba??dwill first try to matchbdand thenbad.Still, why does the regular expression match once for every character in the string (plus one at the end)? Well, an empty match is a valid regular expression. For example, searching for
^or$(the start, respectively end of the string) will yield a match, albeit empty. The same for this “useless” expression, every position inside the tested string is a valid match for the expression that doesn’t impose any constraints on the match.