# Newbie regexp question

**URL:** <https://www.ruby-forum.com/t/newbie-regexp-question/108684>\
**Category:** Ruby\
**Created:** [July 6, 2007, 12:34am UTC](https://www.ruby-forum.com/t/newbie-regexp-question/108684 "2007-07-06T00:34:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 6, 2007, 12:34am UTC](https://www.ruby-forum.com/t/newbie-regexp-question/108684/1 "2007-07-06T00:34:38Z")

</div>

Newbs here, decided to take today to learn regular expression.

What i want to do is take a sentence and just pull two things from it.  
Example:

“My address is 68 Ohio”

I want to pull address and 68 from the sentence but that pesky is is  
getting in my way(or im too newb)

“My address is 68 ohio” =~ /\w{7}\d{2}/ is what i tried but continuous  
nils. Any help?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 6, 2007, 12:45am UTC](https://www.ruby-forum.com/t/newbie-regexp-question/108684/2 "2007-07-06T00:45:38Z")

</div>

On 7/5/07, Skt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Newbs here, decided to take today to learn regular expression.
> 
> What i want to do is take a sentence and just pull two things from it. Example:
> 
> “My address is 68 Ohio”
> 
> I want to pull address and 68 from the sentence but that pesky is is getting in my way(or im too newb)
> 
> “My address is 68 ohio” =~ /\w{7}\d{2}/ is what i tried but continuous nils. Any help?

> > m = “My address is 68 Ohio”.match(/(\w{7}).\*(\d{2})/)  
> > =\> #MatchData:0x33c17c  
> > m[1]  
> > =\> “address”  
> > m[2]  
> > =\> “68”

But what that actually says is 'Match a word 7 characters long,  
followed by zero or more matches of any character (except newline),  
then match two digits.

If you really wanted to pull address and 68, you’d want:

> > m = “My address is 68 Ohio”.match(/(address).\*(68)/)  
> > =\> #MatchData:0x314438  
> > m[1]  
> > =\> “address”  
> > m[2]  
> > =\> “68”

Hope that helps.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 6, 2007, 12:48am UTC](https://www.ruby-forum.com/t/newbie-regexp-question/108684/3 "2007-07-06T00:48:39Z")

</div>

On 7/5/07, Skt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Newbs here, decided to take today to learn regular expression.

You’re brave to try and learn regular expressions in a day. I  
recommend you accept now that it will be a life long journey. (:

> What i want to do is take a sentence and just pull two things from it. Example:
> 
> “My address is 68 Ohio”
> 
> I want to pull address and 68 from the sentence but that pesky is is getting in my way(or im too newb)
> 
> “My address is 68 ohio” =~ /\w{7}\d{2}/ is what i tried but continuous nils. Any help?

What do you think of this: ?  
irb(main):005:0\> s = “My address is 68 ohio”  
=\> “My address is 68 ohio”  
irb(main):006:0\> r = /(address)[^\d]+(\d+)/  
=\> /(address)[^\d]+(\d+)/  
irb(main):007:0\> s.scan( r ) {|match| p match}  
[“address”, “68”]  
=\> “My address is 68 ohio”

The regular expression essentially says: “Find the word address,  
followed by one or more things thats not a number, then also find one  
or more numbers”

hth,  
-Harold

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 6, 2007, 6:24am UTC](https://www.ruby-forum.com/t/newbie-regexp-question/108684/4 "2007-07-06T06:24:04Z")

</div>

> “My address is 68 Ohio”
> 
> I want to pull address and 68 from the sentence but that pesky is is getting in my way(or im too newb)
> 
> “My address is 68 ohio” =~ /\w{7}\d{2}/ is what i tried but continuous nils. Any help?

Here is something like what you tried that will skip the ‘is’.

s = “My address is 68 ohio”  
p s.scan(/\w{7}|\d{2}/)

OR

p s.scan(/address|\d+/)

Harry

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 6, 2007, 1:01am UTC](https://www.ruby-forum.com/t/newbie-regexp-question/108684/5 "2007-07-06T01:01:41Z")

</div>

Hi –

On Fri, 6 Jul 2007, Harold H. wrote:

> > “My address is 68 Ohio”  
> > irb(main):005:0\> s = “My address is 68 ohio”  
> > =\> “My address is 68 ohio”  
> > irb(main):006:0\> r = /(address)[^\d]+(\d+)/

You can also use \D for non-digit.

David
