Regexp help please

P

PDTCT

Hi All,

I am trying to extract quoted text fromlines in a file. Assume I have
read in a line as follows

type "this piece of text" into field called "search field"

I then want to extract the two pieces of quoted text from this line.
I'm halfway there with this
line =~ /(".*?")/
this will match the first piece of quoted text, which I can access
with $1. I'm having trouble getting the match to carry on & match the
next piece of quoted text.

Any help greatly appreciated :)

Many Thanks

Paul
 
R

Robert Dober

453/22 > irb
irb(main):001:0> a=%<"first" and "second">
=> "\"first\" and \"second\""
irb(main):002:0> a.scan /".*?"/
=> ["\"first\"", "\"second\""]
irb(main):003:0>

HTH
Robert
 
D

Dave Bass

Or:

line=%<"first" and "second">
line =~ /(".*?")[^"]*(".*?")/

puts $1 # => "first"
puts $2 # => "second"

The [^"]* means "zero or more characters that aren't a doublequote
character".

Dave
 
P

PDTCT

Thanks Robert, that was exactly what I was looking for. I knew there
had to be a simple way to do it in Ruby. I love Ruby!!

Paul
 

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

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top