I have a string s= XXaaXX, in which i want to replace the last substring
r, which is determined at run time.
r= 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.
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.
Alternatively you can reverse, sub, reverse, but I like Vince’s way.
[SNIP]
OP talked about the last occurence, not an occurence at the end, so
anchoring is not an option
reverse sub reverse is a nice idea, I hope you do not mind me spelling
it
out
s.reverse.sub(r,“”).reverse
and in case U want inline replacement
s.reverse!.sub!(r,“”).reverse! # That is rubyish, isn’t it :]
Cheers
Robert
–
Deux choses sont infinies : l’univers et la bêtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.
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.
Would
s.gsub(/#{r}$/,’’)
do what you want ?
If that’s for a single line string, drop the g and just use sub().
Well Thomas if your pattern is really at the end of the string you can
use
the – much faster –
anchoring approach Vincent suggested, just escape your regexp and use
sub
instead of
gsub as pointed out by Edward in case there is only one line.
but I am afraid that this is much more expensive than the
reverse.sub.reverse trick and much less readable
Robert
Variable length lookahead assertions are a killer, just look at this, I
have
replaced the last < in this page with ***
that is a string of length 2369, look at the benchmark please:
n = 50000
Benchmark.bm do |x|
x.report(“reverse.sub.reverse”) {
n.times do
s1 = string.reverse.sub(r1,"***").reverse
end
}
x.report(“lookahead”) {
n.times do
s2 = string.sub(r2,"***")
end
}
end
user system total real
reverse.sub.reverse 1.470000 0.010000 1.480000 ( 1.483691)
lookahead 48.100000 0.060000 48.160000 ( 52.608063)
As far as I know fixed with lookahead is about ok, though.
Cheers
Robert