Hello,
I’m new to Ruby and I’m fighting with regexp.
I would like to replace ‘test1<\math>’ and ‘test2<\math>’ by
‘ok’ in the string “blabla1 blabla1 test1<\math> blabla2 blabla2
test2<\math>”
When I try this :
@a = “blabla1 blabla1 test1<\math> blabla2 blabla2
test2<\math>”
@b = @a.gsub(/.+<\math>/,‘ok’)
I get “blabla1 blabla1 ok” instead of “blabla1 blabla1 ok blabla2
blabla2 ok”
Could you please help me to tune my regexp so it works the way I want ?
Thank you in advance,
Fred.
fr fred:
When I try this :
@a = "blabla1 blabla1 test1<\math> blabla2 blabla2
test2<\math>"
@b = @a.gsub(/.+<\math>/,‘ok’)
I get “blabla1 blabla1 ok” instead of "blabla1 blabla1 ok blabla2
blabla2 ok"
Could you please help me to tune my regexp so it works the
way I want ?
your code works fine here,
irb(main):001:0> @a = “blabla1 blabla1 test1<\math> blabla2
blabla2
irb(main):002:0” test2<\math>"
=> “blabla1 blabla1 test1 blabla2 blabla2
\ntest2”
irb(main):003:0> @b = @a.gsub(/.+<\math>/,‘ok’)
=> “blabla1 blabla1 ok blabla2 blabla2 \nok”
(ignore the newline; i just copiednpaste fr your email…)
kind regards -botp
Hi –
On Fri, 7 Jul 2006, Fred VD wrote:
@a = “blabla1 blabla1 test1<\math> blabla2 blabla2
test2<\math>”
@b = @a.gsub(/.+<\math>/,‘ok’)
I get “blabla1 blabla1 ok” instead of “blabla1 blabla1 ok blabla2
blabla2 ok”
Could you please help me to tune my regexp so it works the way I want ?
As I suspect seven or eight people will tell you, you need to make
your one-or-more quantifier non-greedy Note the question mark:
a = “blabla1 blabla1 test1<\math> blabla2 blabla2
test2<\math>”
puts a.gsub(/.+?<\math>/,‘ok’)
I’ve made a couple of other changes. In a double-quoted string, “\m”
is just the letter m. If you want a \ you have to escape it with
another . (Are you sure you don’t want a forward slash anyway?)
I have a feeling you don’t really need instance variables here; if
not, it’s better to use locals.
David
On 7/7/06, Fred VD [email protected] wrote:
@a = “blabla1 blabla1 test1<\math> blabla2 blabla2
test2<\math>”
@b = @a.gsub(/.+<\math>/,‘ok’)
I get “blabla1 blabla1 ok” instead of “blabla1 blabla1 ok blabla2
blabla2 ok”
Could you please help me to tune my regexp so it works the way I want ?
You need to replace .+ with .+? - the former is ‘greedy’; i.e. it
matches as much as it possibly can, so it matches everything between
the first and the last <\math>.
martin
On 07/07/06, Fred VD [email protected] wrote:
@a = “blabla1 blabla1 test1<\math> blabla2 blabla2
test2<\math>”
@b = @a.gsub(/.+<\math>/,‘ok’)
I get “blabla1 blabla1 ok” instead of “blabla1 blabla1 ok blabla2
blabla2 ok”
- and * are greedy. Use +? and *? for non-greedy operation:
a = ‘a b c d e f g’
a.gsub(%r!.+?!, ‘ok’)
=> “a b ok d ok f g”
a.gsub(%r!.+!, ‘ok’)
=> “a b ok f g”
A greedy expression will match up to the *last * occurrence of what
follows it in the expression, whereas a non-greedy expression will
match up to the first occurrence.
Paul.
Yes ! The “+?” was what I needed !
Thanks to all for your help.
Fred.
On Jul 7, 2006, at 11:52, Peña, Botp wrote:
Could you please help me to tune my regexp so it works the
=> “blabla1 blabla1 ok blabla2 blabla2 \nok”
(ignore the newline; i just copiednpaste fr your email…)
I thought this might warrant some explanation in light of the later
posts about greedy operators.
The newline from the paste is what makes this work differently from
the normal example - greedy expressions (already mentioned) aren’t
greedy across newlines unless you specify a multiline regex, so the
newline is what made the regex appear correct, when it wouldn’t be in
a general case.
with newline
a = “blabla1 blabla1 test1 blabla2 blabla2
\ntest2”
a.gsub(/.+<\math>/,‘ok’)
=> “blabla1 blabla1 ok blabla2 blabla2 \nok”
sans newline
b = “blabla1 blabla1 test1 blabla2 blabla2
test2”
b.gsub(/.+<\math>/,‘ok’)
=> “blabla1 blabla1 ok”
matthew smillie.