Regexp in Ruby

T

Teme Rosi

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?
 
T

Tim Mcd

Teme said:
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
 
T

Tim Mcd

Tim said:
Teme said:
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.
 
J

Jesús Gabriel y Galán

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.
 
T

Tim Greer

Tim said:
Teme said:
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.
 
T

Tim Greer

Teme said:
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.
 
W

William James

Teme said:
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"
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top