Regexp in Ruby

Discussion in 'Ruby' started by Teme Rosi, Jan 14, 2009.

  1. Teme Rosi

    Teme Rosi Guest

    names = ["My name is Jack"]
    if names =~ /My name is (\w+)/i
    puts $1
    end


    What im trying to do with this code is to print rest of the line of the
    same line where "My name is" is. But it doesnt work. Can anyone help?
    --
    Posted via http://www.ruby-forum.com/.
     
    Teme Rosi, Jan 14, 2009
    #1
    1. Advertising

  2. Teme Rosi

    Tim Mcd Guest

    Teme Rosi wrote:
    > names = ["My name is Jack"]
    > if names =~ /My name is (\w+)/i
    > puts $1
    > end
    >
    >
    > What im trying to do with this code is to print rest of the line of the
    > same line where "My name is" is. But it doesnt work. Can anyone help?


    Hmm, that works for me. I don't know what the /i is for tho. If you need
    to tset out regexp's try www.rubular.com
    --
    Posted via http://www.ruby-forum.com/.
     
    Tim Mcd, Jan 14, 2009
    #2
    1. Advertising

  3. Teme Rosi

    Tim Mcd Guest

    Tim Mcd wrote:
    > Teme Rosi wrote:
    >> names = ["My name is Jack"]
    >> if names =~ /My name is (\w+)/i
    >> puts $1
    >> end
    >>
    >>
    >> What im trying to do with this code is to print rest of the line of the
    >> same line where "My name is" is. But it doesnt work. Can anyone help?

    >
    > Hmm, that works for me. I don't know what the /i is for tho. If you need
    > to tset out regexp's try www.rubular.com


    Wait a second! Could your problem be this: names is an array. names =~
    /yourregexp/ would be trying to match the array. Try names[0] =~ /My
    name is (\w+)/i

    Tim.
    --
    Posted via http://www.ruby-forum.com/.
     
    Tim Mcd, Jan 14, 2009
    #3
  4. On Wed, Jan 14, 2009 at 6:41 PM, Teme Rosi <> wrote:
    > names = ["My name is Jack"]
    > if names =~ /My name is (\w+)/i
    > puts $1
    > end
    >
    >
    > What im trying to do with this code is to print rest of the line of the
    > same line where "My name is" is. But it doesnt work. Can anyone help?


    In your snippet, names is an array, so the method =~ doesn't do
    anything meaningful.
    You need to call that on the string like this:

    irb(main):019:0> names = "My name is Jack"
    => "My name is Jack"
    irb(main):020:0> if names =~ /My name is (\w+)/i
    irb(main):021:1> puts $1
    irb(main):022:1> end
    Jack

    Jesus.
     
    Jesús Gabriel y Galán, Jan 14, 2009
    #4
  5. Teme Rosi

    Tim Greer Guest

    Tim Mcd wrote:

    > Teme Rosi wrote:
    >> names = ["My name is Jack"]
    >> if names =~ /My name is (\w+)/i
    >> puts $1
    >> end
    >>
    >>
    >> What im trying to do with this code is to print rest of the line of
    >> the same line where "My name is" is. But it doesnt work. Can anyone
    >> help?

    >
    > Hmm, that works for me. I don't know what the /i is for tho. If you
    > need to tset out regexp's try www.rubular.com


    /i means case insensitive matching.
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Jan 14, 2009
    #5
  6. Teme Rosi

    Tim Greer Guest

    Teme Rosi wrote:

    > names = ["My name is Jack"]
    > if names =~ /My name is (\w+)/i
    > puts $1
    > end
    >
    >
    > What im trying to do with this code is to print rest of the line of
    > the same line where "My name is" is. But it doesnt work. Can anyone
    > help?


    I assume you meant names = "My name is Jack" so it's a string, rather
    than an array.
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Jan 14, 2009
    #6
  7. Teme Rosi

    Teme Rosi Guest

    Teme Rosi, Jan 14, 2009
    #7
  8. Teme Rosi wrote:

    > names = ["My name is Jack"]
    > if names =~ /My name is (\w+)/i
    > puts $1
    > end
    >
    >
    > What im trying to do with this code is to print rest of the line of
    > the same line where "My name is" is. But it doesnt work. Can anyone
    > help?


    s = "My name is Jack Frost"
    ==>"My name is Jack Frost"
    s[ /My name is (.*)/, 1 ]
    ==>"Jack Frost"
     
    William James, Jan 15, 2009
    #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. Greg Hurrell
    Replies:
    4
    Views:
    167
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    508
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    381
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    196
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    192
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page