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”
  irb(main):007:0> str.scan(/password:([^\s]+)/)[0]
  => [“banana”]
Aha, ‘scan’, that’s great thanks.
Yeah, though not really the most sensible in this case. Also you’d have
to do
[0][0] to acutally get “banana”. I’d only use scan when you actually
want
multiple matches. Here I’d use match or String#[]:
string[/password:(\S+)/, 1]
=> “banana”