Regexp in Ruby

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 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/\.

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.

···

On Wed, Jan 14, 2009 at 6:41 PM, Teme Rosi <the_beaf@hotmail.com> 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?

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!

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"

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 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!

Yes, thanks for your help, it works now.

···

--
Posted via http://www.ruby-forum.com/.