Ruby Sub Regular Expression

Discussion in 'Ruby' started by Dave Roberts, Nov 12, 2008.

  1. Dave Roberts

    Dave Roberts Guest

    Hello! I have a string: "Hello - 1 - World"

    irb(main):017:0> a = "Hello - 1 - World"
    => "Hello - 1 - World"

    I want to chop off the "Hello - " part. Hello could be any word, so I
    want to match it generically.
    To match the first minus I do this:

    irb(main):018:0> b = a.sub(/\s-\s/, "")
    => "Hello1 - World"

    Good! Now to get rid of the first word I try this:

    irb(main):019:0> c = a.sub(/.*\s-\s/, "")
    => "World"

    Bad! It matched to the second minus! Why does sub do this? I thought
    it was supposed to match the first occurrence only.
    --
    Posted via http://www.ruby-forum.com/.
    Dave Roberts, Nov 12, 2008
    #1
    1. Advertising

  2. Hi. You missed out your non-greedy operator (if thats the right term) in
    your regex.

    Try:

    a = "Hello - 1 - World"

    c = a.sub(/.*?\s-\s/, "")

    irb(main):004:0> c = a.sub(/.*?\s-\s/, "")
    => "1 - World"


    Hope that helps
    Jim









    Dave Roberts wrote:
    > Hello! I have a string: "Hello - 1 - World"
    >
    > irb(main):017:0> a = "Hello - 1 - World"
    > => "Hello - 1 - World"
    >
    > I want to chop off the "Hello - " part. Hello could be any word, so I
    > want to match it generically.
    > To match the first minus I do this:
    >
    > irb(main):018:0> b = a.sub(/\s-\s/, "")
    > => "Hello1 - World"
    >
    > Good! Now to get rid of the first word I try this:
    >
    > irb(main):019:0> c = a.sub(/.*\s-\s/, "")
    > => "World"
    >
    > Bad! It matched to the second minus! Why does sub do this? I thought
    > it was supposed to match the first occurrence only.
    >
    > ------------------------------------------------------------------------
    >
    >
    > No virus found in this incoming message.
    > Checked by AVG - http://www.avg.com
    > Version: 8.0.175 / Virus Database: 270.9.0/1777 - Release Date: 09/11/2008 09:53
    >
    >
    Jim McKerchar, Nov 12, 2008
    #2
    1. Advertising

  3. On Wed, Nov 12, 2008 at 4:52 PM, Dave Roberts <> wrote:
    > Hello! I have a string: "Hello - 1 - World"
    >
    > irb(main):017:0> a = "Hello - 1 - World"
    > => "Hello - 1 - World"
    >
    > I want to chop off the "Hello - " part. Hello could be any word, so I
    > want to match it generically.
    > To match the first minus I do this:
    >
    > irb(main):018:0> b = a.sub(/\s-\s/, "")
    > => "Hello1 - World"
    >
    > Good! Now to get rid of the first word I try this:
    >
    > irb(main):019:0> c = a.sub(/.*\s-\s/, "")
    > => "World"
    >
    > Bad! It matched to the second minus! Why does sub do this? I thought
    > it was supposed to match the first occurrence only.


    Because * by default is greedy, so it tries to match as much as it can.
    Try this:

    irb(main):002:0> c = a.sub(/.*?\s-\s/, "")
    => "1 - World"

    Jesus.
    Jesús Gabriel y Galán, Nov 12, 2008
    #3
  4. Dave Roberts

    Ben Walton Guest

    Ben Walton, Nov 12, 2008
    #4
  5. Dave Roberts

    Dave Roberts Guest

    Dave Roberts, Nov 12, 2008
    #5
  6. On 12.11.2008 17:14, Jesús Gabriel y Galán wrote:
    > On Wed, Nov 12, 2008 at 4:52 PM, Dave Roberts <> wrote:
    >> Hello! I have a string: "Hello - 1 - World"
    >>
    >> irb(main):017:0> a = "Hello - 1 - World"
    >> => "Hello - 1 - World"
    >>
    >> I want to chop off the "Hello - " part. Hello could be any word, so I
    >> want to match it generically.
    >> To match the first minus I do this:
    >>
    >> irb(main):018:0> b = a.sub(/\s-\s/, "")
    >> => "Hello1 - World"
    >>
    >> Good! Now to get rid of the first word I try this:
    >>
    >> irb(main):019:0> c = a.sub(/.*\s-\s/, "")
    >> => "World"
    >>
    >> Bad! It matched to the second minus! Why does sub do this? I thought
    >> it was supposed to match the first occurrence only.

    >
    > Because * by default is greedy, so it tries to match as much as it can.
    > Try this:
    >
    > irb(main):002:0> c = a.sub(/.*?\s-\s/, "")
    > => "1 - World"


    Other variants would be

    irb(main):001:0> a = "Hello - 1 - World"
    => "Hello - 1 - World"
    irb(main):002:0> a[/\d+\s+-.*/]
    => "1 - World"
    irb(main):003:0> a.sub /^\S+\s+-\s+/, ''
    => "1 - World"
    irb(main):004:0> a.sub /^\w+\s+-\s+/, ''
    => "1 - World"
    irb(main):005:0>

    Cheers

    robert
    Robert Klemme, Nov 12, 2008
    #6
  7. Dave Roberts

    Henrik Nyh Guest

    On Wed, Nov 12, 2008 at 5:11 PM, Jim McKerchar
    <> wrote:
    > Hi. You missed out your non-greedy operator (if thats the right term)


    "lazy"
    Henrik Nyh, Nov 14, 2008
    #7
  8. Thanks Henrik

    Henrik Nyh wrote:
    > On Wed, Nov 12, 2008 at 5:11 PM, Jim McKerchar
    > <> wrote:
    >
    >> Hi. You missed out your non-greedy operator (if thats the right term)
    >>

    >
    > "lazy"
    >
    > ------------------------------------------------------------------------
    >
    >
    > Internal Virus Database is out of date.
    > Checked by AVG - http://www.avg.com
    > Version: 8.0.175 / Virus Database: 270.9.0/1777 - Release Date: 09/11/2008 09:53
    >
    >
    Jim McKerchar, Nov 14, 2008
    #8
    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. VSK
    Replies:
    2
    Views:
    2,272
  2. Frank Wallingford

    Sub-sub-expression evaluation order

    Frank Wallingford, Dec 9, 2004, in forum: C Programming
    Replies:
    4
    Views:
    335
    Chris Torek
    Dec 10, 2004
  3. Ben
    Replies:
    2
    Views:
    864
  4. Davy
    Replies:
    1
    Views:
    317
    Rick Dooling
    Dec 14, 2007
  5. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    1,974
    Lawrence D'Oliveiro
    May 20, 2011
Loading...

Share This Page