[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

simple regexp help needed - pull phrase out of string

Max Williams

10/9/2008 4:32:00 PM

Hey folks. I feel like this should be easy but it's been a long week
and my brain hurts.

If i have a string like this:

string = "pending: sent at Thu Oct 09 17:25:08 +0100 2008,
password:banana errors: couldn't find user with id 230"

and i want to pull out 'banana', what's an elegant way to do it? The
password will always be followed by a space.

I can think of this:
string.match(/password:[^\s]+/)[0].gsub("password:","")

but it seems a bit clumsy and error prone. Is there a nicer way?
(Passwords can contain non alphanumeric characters btw)
--
Posted via http://www.ruby-....

5 Answers

Rob Biedenharn

10/9/2008 4:38:00 PM

0


On Oct 9, 2008, at 12:31 PM, Max Williams wrote:

> Hey folks. I feel like this should be easy but it's been a long week
> and my brain hurts.
>
> If i have a string like this:
>
> string = "pending: sent at Thu Oct 09 17:25:08 +0100 2008,
> password:banana errors: couldn't find user with id 230"
>
> and i want to pull out 'banana', what's an elegant way to do it? The
> password will always be followed by a space.
>
> I can think of this:
> string.match(/password:[^\s]+/)[0].gsub("password:","")
>
> but it seems a bit clumsy and error prone. Is there a nicer way?
> (Passwords can contain non alphanumeric characters btw)

irb> string = "pending: sent at Thu Oct 09 17:25:08 +0100 2008,
password:banana errors: couldn't find user with id 230"
=> "pending: sent at Thu Oct 09 17:25:08 +0100 2008,\npassword:banana
errors: couldn't find user with id 230"
irb> string.match(/password:(.*) errors:/)[1]
=> "banana"

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com


Patrick He

10/10/2008 3:34:00 AM

0

Hello Max,

Maybe you can try this regex pattern:

irb(main):007:0> str.scan(/password:([^\s]+)/)[0]
=> ["banana"]


Regards,
Patrick


Max Williams wrote:
> Hey folks. I feel like this should be easy but it's been a long week
> and my brain hurts.
>
> If i have a string like this:
>
> string = "pending: sent at Thu Oct 09 17:25:08 +0100 2008,
> password:banana errors: couldn't find user with id 230"
>
> and i want to pull out 'banana', what's an elegant way to do it? The
> password will always be followed by a space.
>
> I can think of this:
> string.match(/password:[^\s]+/)[0].gsub("password:","")
>
> but it seems a bit clumsy and error prone. Is there a nicer way?
> (Passwords can contain non alphanumeric characters btw)
>

Max Williams

10/10/2008 9:04:00 AM

0

Patrick He wrote:
> Hello Max,
>
> Maybe you can try this regex pattern:
>
> irb(main):007:0> str.scan(/password:([^\s]+)/)[0]
> => ["banana"]
>
>
> Regards,
> Patrick

Aha, 'scan', that's great thanks.
--
Posted via http://www.ruby-....

Sebastian Hungerecker

10/10/2008 9:12:00 AM

0

Max Williams wrote:
> > =C2=A0 =C2=A0 irb(main):007:0> str.scan(/password:([^\s]+)/)[0]
> > =C2=A0 =C2=A0 =3D> ["banana"]
>
> Aha, 'scan', that's great thanks.

Yeah, though not really the most sensible in this case. Also you'd have to =
do=20
[0][0] to acutally get "banana". I'd only use scan when you actually want=20
multiple matches. Here I'd use match or String#[]:
string[/password:(\S+)/, 1]
=3D> "banana"

=2D-=20
NP: Die Apokalyptischen Reiter - Der Weg
Jabber: sepp2k@jabber.org
ICQ: 205544826

Max Williams

10/10/2008 1:23:00 PM

0

Even better, many thanks.
--
Posted via http://www.ruby-....