Regular expression question

  • Thread starter George Moschovitis
  • Start date
G

George Moschovitis

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
 
D

David A. Black

Hi --

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
 
M

Mark Sparshatt

George said:
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
 
S

Shajith CT

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 http://www.regular-expressions.info/repeat.html
(Parent site's a pretty good regex reference)

-CT
 
J

John Johnson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top