Regular expression question

Discussion in 'Ruby' started by George Moschovitis, Jul 4, 2004.

  1. 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
     
    George Moschovitis, Jul 4, 2004
    #1
    1. Advertising

  2. 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
     
    David A. Black, Jul 4, 2004
    #2
    1. Advertising

  3. 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
     
    Mark Sparshatt, Jul 4, 2004
    #3
  4. George Moschovitis

    Shajith CT Guest

    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

    On Sun, 4 Jul 2004 21:22:43 +0900, 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
    >
    >
     
    Shajith CT, Jul 4, 2004
    #4
  5. George Moschovitis

    John Johnson Guest

    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
    >
     
    John Johnson, Jul 4, 2004
    #5
  6. Thank you all for the answer!

    George Moschovitis
     
    George Moschovitis, Jul 5, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Munn

    Regular expression question...

    Andrew Munn, Jun 29, 2003, in forum: Perl
    Replies:
    1
    Views:
    2,161
    rakesh sharma
    Jun 30, 2003
  2. Glenn Kidd

    Regular expression question

    Glenn Kidd, Aug 18, 2003, in forum: Perl
    Replies:
    0
    Views:
    942
    Glenn Kidd
    Aug 18, 2003
  3. VSK
    Replies:
    2
    Views:
    2,353
  4. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    872
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    12,024
    vforvikash
    Dec 29, 2008
Loading...

Share This Page