From: Robert Feldt Date: 2004-04-07T07:29:48+09:00 Subject: Re: Bug in sub? Ara.T.Howard wrote: >>Sorry, call me thick but I don't understand that :-) >> >>Did Guy confirm that it was a bug, or show that it is not a bug? >> >> > > __not_a_bug__ # that's python for not a bug > > >svg% ruby -e 'p "abcdef".sub(/(.)./, "\\+")' >"acdef" >svg% > > > /(.)./ and '\+' > > - match a char, followed by another char. remember the first char > - replace the entire match with the remembered first char > > abcdef -> acdef > -- > - - > >svg% ruby -e 'p "abcdef".sub(/(.).(..)/, "\\+")' >"cdef" >svg% > > > /(.).(..)/ and '\+' > > - match a char, followed by another char. followed by two more chars. > remember the last two chars (and the first) > - replace the entire match with the remembered last two chars (which is > the 'last matched group') > > abcdef -> cdef > ---- > -- -- > > > > Yes, and further: If you want to do substitutions and aren't sure there will be no backslash sequences in the replacement string you should use the block form, like so: $ ruby -e 'p "a".sub("a") {"\\+"}' "\\+" I added a note to my code review checklist to "always" use the block form... ;) Sorry for wasting bandwidth on this; I should have read the docs more closely. /Robert