On Thu, Dec 08, 2011 at 03:50:39PM +0100, Janis Papanagnou wrote:
> s=ababxyabab
> echo ${s/%+-(ab)/AB}
> ababxyAB <-- Why not the shortest match just like ${s/#+-(ab)/AB} ?
No bug (unless the ksh spec is out to surprise me :)
Translated to Perl, it's the same output:
perl -e '$_="ababxyabab"; s/(ab)+$/AB/; print' ==
perl -e '$_="ababxyabab"; s/(ab)+?$/AB/; print' ==
ababxyAB
My take would be:
Consider a simple-minded forward automaton for matching: anchor match
at first ab, extend match non-greedy (2nd line) to EOL. Backtrack on
failure and go forward to next ab match. Same results as above.
Now if we start the match earlier with an additional greedy/non-greedy
match, things will differ (note the greed "inversion" :) ):
perl -e '$_="ababxyabab"; s/(.*)(ab)+$/${1}AB/; print' == ababxyabAB
perl -e '$_="ababxyabab"; s/(.*?)(ab)+$/${1}AB/; print' == ababxyAB
Thus:
The behaviour is what I'd expect, though the EOL-anchoring syntax of
ksh (% being first) makes the expression harder to read and the wrong
expectation seem 'natural' in comparison to #, hiding the obvious
differences between ^ and $ aka # and % in variable substitution.
--
cu
Peter l Jakobi
[email protected]
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users