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

Help please 8
Please help 7
DUPLICATE MODS, PLEASE DELETE, SORRY! 1
Please, help me. 1
Code help please 4
Please help me!!! 3
Help for extracting text with regexp. 4
Please Help! 1

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top