|
From: PDTCT on 5 Jul 2008 05:27 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
From: Robert Dober on 5 Jul 2008 05:39 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 -- http://ruby-smalltalk.blogspot.com/ --- AALST (n.) One who changes his name to be further to the front D.Adams; The Meaning of LIFF
From: Dave Bass on 5 Jul 2008 07:20 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 -- Posted via http://www.ruby-forum.com/.
From: PDTCT on 5 Jul 2008 08:30 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
|
Pages: 1 Prev: title avoiding spurious response Next: sqlite3 Re: title avoiding spurious response |