Newbie regexp question

S

Skt

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. Exam=
ple:

"My address is 68 Ohio"

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

"My address is 68 ohio" =3D~ /\w{7}\d{2}/ is what i tried but continuous ni=
ls. Any help?=
 
G

Gregory Brown

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})/)
=> # said:
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)/)
=> # said:
m[1] => "address"
m[2]
=> "68"

Hope that helps.
 
H

Harold Hausman

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
 
D

dblack

Hi --

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+)/

You can also use \D for non-digit.


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 
H

Harry Kakueki

"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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top