Re: simple regexp help needed - pull phrase out of string



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)


.



Relevant Pages

  • Re: simple regexp help needed - pull phrase out of string
    ... On Oct 9, 2008, at 12:31 PM, Max Williams wrote: ... and my brain hurts. ... If i have a string like this: ... (Passwords can contain non alphanumeric characters btw) ...
    (comp.lang.ruby)
  • Re: A string & a list
    ... You should exclude those elements from the string: ... The array @- is an internal one, storing the offsets of the substrings found by the last regex pattern match. ... The push of the results of the recursive call is qualified by a call to grepto make sure the new value isn't already in the list. ...
    (perl.beginners)
  • Re: REGEX.Replace Question
    ... >> We are not talking about string literals. ... > to the static Regex.Replace function is a regex pattern. ... you'll realize that there's no way the replacement string can ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Bill Frisell
    ... ...stop with the foot pedals and string ... a coherent solo together...sheesh! ... Ouch, my brain hurts. ...
    (rec.music.gdead)
  • Re: IEEE-FP 0.5.3
    ... > David N. Williams wrote: ... REPRESENT places a string of "u" characters in the buffer ... This is a common interpretation of the Standard definition, ...
    (comp.lang.forth)