# Simple regexp help needed - pull phrase out of string

**URL:** <https://www.ruby-forum.com/t/simple-regexp-help-needed-pull-phrase-out-of-string/150542>\
**Category:** Ruby\
**Created:** [October 9, 2008, 6:34pm UTC](https://www.ruby-forum.com/t/simple-regexp-help-needed-pull-phrase-out-of-string/150542 "2008-10-09T18:34:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 9, 2008, 6:34pm UTC](https://www.ruby-forum.com/t/simple-regexp-help-needed-pull-phrase-out-of-string/150542/1 "2008-10-09T18:34:15Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 9, 2008, 6:41pm UTC](https://www.ruby-forum.com/t/simple-regexp-help-needed-pull-phrase-out-of-string/150542/2 "2008-10-09T18:41:39Z")

</div>

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

> 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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 10, 2008, 6:04am UTC](https://www.ruby-forum.com/t/simple-regexp-help-needed-pull-phrase-out-of-string/150542/3 "2008-10-10T06:04:23Z")

</div>

Hello Max,

Maybe you can try this regex pattern:

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

```

Regards,  
Patrick

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 10, 2008, 11:15am UTC](https://www.ruby-forum.com/t/simple-regexp-help-needed-pull-phrase-out-of-string/150542/4 "2008-10-10T11:15:40Z")

</div>

Max W. wrote:

> > Â Â 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”

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 10, 2008, 11:05am UTC](https://www.ruby-forum.com/t/simple-regexp-help-needed-pull-phrase-out-of-string/150542/5 "2008-10-10T11:05:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 10, 2008, 3:25pm UTC](https://www.ruby-forum.com/t/simple-regexp-help-needed-pull-phrase-out-of-string/150542/6 "2008-10-10T15:25:26Z")

</div>

Even better, many thanks.
