On Tue, Nov 22, 2005 at 01:09:40AM +0100, Ruud H.G. van Tol wrote: > >>>> 's/$/foo/' becomes 's/<after .*>/foo/' > >>> > >>> Uh, no, because <after> is still a zero width assertion. :-) > >> > >> That's why I chose it. It is not at the end-of-string? > > > > Because ".*" matches "", /<after .*>/ would be true at > > every position in the string, including the beginning, > > and this is where "foo" would be substituted. > > I expected greediness, also because <after .*?> could behave non-greedy. > ... > But why does <after .*> behave non-greedy?
I think you may be misreading what <after .*> does -- it's a lookbehind assertion. An assertion such as <after pattern> attempts to match pattern to the sequence immediately preceding the current match position. It does not mean "skip over pattern and then match whatever comes afterwards". The greediness of the .* subpattern in <after .*> doesn't affect things at all -- <after .*> is still a zero-width assertion. Since ".*" can match at every position, <after .*> will be a successful zero-width match (i.e., a null string) at every position in the target string, including the beginning. So, s/<after .*>/foo/ matches the first null string it finds -- the one at the beginning of the string -- and replaces it with "foo". It's the same as if you had written s/<null>/foo/, since <after .*> and <null> will both end up matching exactly the same (i.e., a zero-width string at any position). If this still doesn't make any sense, contact me off-list and I'll try and explain it there. Pm