Consider a regex language with the greedy quantifier $*$, the nongreedy quantifier ${*}?$, ordered alternation, and character classes. (This is essentially a sublanguage of PCRE without backreferences, look-around assertions, or some of the other fancier bits.)
A match $[a_0,a_1)$ for for a regex $R$ on a string $s = s_0\dots s_n$ is a half-open interval over $\mathbb{N}$ such that $s_{a_0}\dots s_{a_1-1}$ is accepted by $R$.
We give a recursive definition of what makes one match better than another. A match $a = [a_0,a_1)$ for regex $R$ on a string is better than another match $b = [b_0,b_1)$ if $a_0 < b_0$ or, if $a_0 = b_0$ and:
If $R$ is a character class: Character classes have unique matches, so all matches at the same position for $R$ are equal. Hence this case is impossible.
If $R = ST$:
- The leading portion of $a$ is a better match for $S$ than the leading portion of $b$, or
- The leading portions of $a$ and $b$ are equally good matches for $S$, and the trailing portion of $a$ is a better match for $T$ than the trailing portion of $b$.
If $R = S|T$:
- $a$ is a match for $S$ and $b$ is not, or
- $a$ and $b$ are equally good matches for $S$ and $a$ is a better match for $S$ than $b$ is, or
- $a$ and $b$ are not matches for $S$ but are matches for $T$, and $a$ is a better match for $T$ than $b$ is.
All other syntactic forms reduce to the above three for purposes of match priority:
- $R = S{*}$: $R \equiv S^0|S^1|\dots$
- $R = S{*}?$: $R \equiv \dots|S^1|S^0$
These infinitary patterns are used for purposes of match priority only---they are not part of the match language under consideration.
The "better" relation is a weak linear order over all possible matches for a given pattern.
Call two regexes $S,T$ match-equivalent if, for every finite input string, the set of pairwise disjoint best matches for $S$ equals the set of pairwise disjoint best matches for $T$.
Q: Is it the case that for every regex $S$ containing the nongreedy quantifier ${*}?$ there is a match-equivalent regex $T$ which contains no nongreedy quantifiers?
Edit: This is a complete rewrite of the question to clarify what was being asked.
\tt
does not prevent LaTeX from interpreting special characters and control sequences!) $\endgroup$a+?
) is still {a^n: n≥1}. If you perform an unanchored regex match (such as'aaaa' =~ /a+?/
in Perl), you will not getaaaa
as a result, but that is just because branches are tried in a different order froma+
. If you do it appropriately with anchors (such as'aaaa' =~ /^a+?\z/
in Perl), you getaaaa
as a result. $\endgroup$//g
in Perl) would return? $\endgroup$