Regular expression question

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!

George Moschovitis

···

--
http://www.joy.gr

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 :slight_smile: 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

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>')

try this

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

at the moment the .* is being too greedy when it's matching, causing it to match
Text{/b} kokoko {b}Lalal
Using .*? switches off the greedy matching so it only matches up to the first {/b}

HTH

···

--
Mark Sparshatt

Hiya!

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

Note the '?' in the regular expression: to make the match non-greedy.
Which in English means that it will match till the first "{/b}", and
not till the last possible one, which is the default case for some
reason. Which is why your regex was matching the longest possible
match of <b>...</b> in the string.

See Regex Tutorial - Repetition with Star and Plus
(Parent site's a pretty good regex reference)

-CT

···

On Sun, 4 Jul 2004 21:22:43 +0900, George Moschovitis <gm@navel.gr> 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!

George Moschovitis

--
http://www.joy.gr

I realize you've gotten what is probably the most correct answer,
but here are a couple of alternatives:

str.gsub(/\{/, '<');
str.gsub(/\}/, '>');

or better still

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

Which has the advantage of being a more general case, i.e. it will
work on {h1}, {center} or anything else in braces.

Regards,
   JJ

···

On Sunday, July 4, 2004, at 08:22 AM, 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!

George Moschovitis

--
http://www.joy.gr

Thank you all for the answer!

George Moschovitis

I realize you've gotten what is probably the most correct answer,
but here are a couple of alternatives:

str.gsub(/\{/, '<');
str.gsub(/\}/, '>');

You would need .gsub! here, but str.tr('{}', '<>') is even better.
(BTW, please drop the ;.)

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

Which has the advantage of being a more general case, i.e. it will
work on {h1}, {center} or anything else in braces.

Be careful, this can be used to do XSS attacks.

Best thing would be IMHO:
    str.gsub!(/\{(\w+)\}/, '<\1>')

Alternatively, replace \w+ with a list of allowed tags: b|i|strong|em

Regards,
   JJ

Christian Neukirchen
<chneukirchen@gmail.com>

···

On Mon, 5 Jul 2004 06:42:23 +0900, John Johnson <johnatl@mac.com> wrote: