[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regexp in Ruby

Teme Rosi

1/14/2009 5:42:00 PM

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

7 Answers

Tim Mcd

1/14/2009 5:44:00 PM

0

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

Tim Mcd

1/14/2009 5:48:00 PM

0

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

Jesús Gabriel y Galán

1/14/2009 5:49:00 PM

0

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?

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.

Tim Greer

1/14/2009 6:04:00 PM

0

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

1/14/2009 6:04:00 PM

0

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

1/14/2009 6:19:00 PM

0

Yes, thanks for your help, it works now.
--
Posted via http://www.ruby-....

William James

1/15/2009 10:43:00 AM

0

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"