I don't think I can explain it better than man.txt but I'll give it a shot. The first thing you have to understand is that the two ?'s in ?? do not do the same thing. The "?" character, when following a subpattern (which could be (?:abc|d), [a-z], . or whatever) it tells regex to match "zero or one time". IE, $regex(a,/^ab?/) returns one, because "ab?" matches "a once and b zero or one time." This means it matches "a" or "ab" respectively.

But this poses another question: which subpattern is tried first against the input string? "a" or "ab" ? In other words, does $regex() try to see if "ab" matches the input and when this fails THEN try "a" ? Or the opposite? The answer is that it depends on whether the second "?" was used. If not, $regex() tries "ab" first and if it doesn't match, it tries "a". This is the default behaviour of quantifiers (such as ?, *, +, {4,} etc): they try to match as much as possible. But if a "?" is appended to the quantifier, it makes the quantifier match as little as possible. This means that the input is first checked against "a" and if it doesn't match for some reason (and that reason is that the part of the regex following "eats" the "b"), it is checked against "ab". Here's an example to see the difference:

//!.echo -q $regex(ab,/^(ab?)/) | echo -s $regml(1)
it echoes "ab". This means that "ab" was first tried and succeded.

//!.echo -q $regex(ab,/^(ab??)/) | echo -s $regml(1)
this one echoes "a". This means that $regex() first tried "a" (because of the 2nd "?"), it saw that it matches and stopped, ie didn't try "ab".

//!.echo -q $regex(ab,/^(ab??)$/) | echo -s $regml(1)
this one echoes "ab", even though the 2nd "?" was used. That's because the "^" and "$" around (ab??) tell $regex() that the pattern must match the entire input string. But "a" doesn't match the entire string (which is ab) so $regex() then tries "ab", that matches.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com