Hi --

On Sun, 4 Jul 2004, George Moschovitis wrote:

> Hello everyone!
> 
> I have a question regarding regular expressions. Let me explain
> with an example. I have the string:
> 
> "{b}Text{/b} kokoko {b}Lalal{/b}"
> 
> and i want to convert it to this string
> 
> "<b>Text</b> kokoko <b>Lalal</b>"
> 
> if I use the following gsub:
> 
> str.gsub!(/\{b\}(.*)\{\/b\}/, '<b>\1</b>')
> 
> I get:
> 
> "<b>Text{/b} kokoko {b}Lalal</b>"
> 
> not what I want. If I use
> 
> str.gsub!(/\{b\}([^\{]*)\{\/b\}/, '<b>\1</b>')
> 
> I get the correct anwser, but i dont like this
> regular expression. Does ruby support an equivalent
> to the /g option that perl provides? If not is there
> a workaround? I would like to use something like:
> 
> str.gsub!(/\{b\}(.*)\{\/b\}/g, '<b>\1</b>')
> 
> Any help will be apreciated!

Actually gsub *is* the /g equivalent :-) The problem is that you need
to make the .* match non-greedy:

  str.gsub!(/\{b}(.*?)\{\/b}/, '<b>\1</b>')

(You'd have to do this with /g in Perl too.)


David

-- 
David A. Black
dblack / wobblini.net