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:
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:
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}
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.
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:
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: