From: Robert Dober Date: 2006-07-27T04:05:19+09:00 Subject: Re: gsub ------=_Part_30692_3314265.1153940714487 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 7/26/06, Guillaume Carbonneau wrote: > > Hi, what you really want to use if you want a clean solution is negative > lookaheads built into your regular expression. > > How lookaheads work: > http://www.regular-expressions.info/lookaround.html > > Search google for specific lookahead uses in Ruby as I haven't experience= d > them yet with this language. Well that is nice ;) actually that would be something like s.sub( %r{#{Regexp.escape(r)}(?!.*#{Regexp.escape(r)})},"") but I am afraid that this is much more expensive than the reverse.sub.reverse trick and much less readable Robert On 7/26/06, Tomas Fischer wrote: > > > > Hi, > > > > I have a string s=3D XXaaXX, in which i want to replace the last substr= ing > > r, which is determined at run time. > > > > r=3D XX > > > > s.gsub(/XX\z/,'') solves the problem in a static way. > > s.gsub(r,'') solves it too, but who can I specify, that only the last > > occurence should be replaced. > > > > Best regards, > > Tomas > > > > > > -- > > Posted via http://www.ruby-forum.com/. > > > > > > --=20 Deux choses sont infinies : l'univers et la b=EAtise humaine ; en ce qui concerne l'univers, je n'en ai pas acquis la certitude absolue. - Albert Einstein ------=_Part_30692_3314265.1153940714487--