From: Robert Klemme Date: 2012-10-06T19:12:57+09:00 Subject: Re: Regular Expression Help On Sat, Oct 6, 2012 at 1:21 AM, wrote: > 1.9.3-p194 :001 > text = '"--- this is a string ---" = "--- this is a string > ---";' > => "\"--- this is a string ---\" = \"--- this is a string ---\";" > 1.9.3-p194 :002 > text.scan(/"(.*?)" = "(.*?)"/).flatten > => ["--- this is a string ---", "--- this is a string ---"] I'd rather use an approach which explicitly includes the "=" sign, for example irb(main):026:0> text = '"--- this is a string ---" = "--- this is a string ---";' => "\"--- this is a string ---\" = \"--- this is a string ---\";" irb(main):027:0> if %r{"([^"]*)"\s*=\s*"([^"]*)"} =~ text then p $1, $2 end "--- this is a string ---" "--- this is a string ---" => ["--- this is a string ---", "--- this is a string ---"] If one wants to allow escaped quotes inside the string things get a bit more tricky but that's doable as well: irb(main):028:0> text = '"--- this is an escaped quote: \"" = "--- this is a string ---";' => "\"--- this is an escaped quote: \\\"\" = \"--- this is a string ---\";" irb(main):029:0> if %r{"((?:\\.|[^"\\])*)"\s*=\s*"((?:\\.|[^"\\])*)"} =~ text then p $1, $2 end "--- this is an escaped quote: \\\"" "--- this is a string ---" => ["--- this is an escaped quote: \\\"", "--- this is a string ---"] Of course, you could also include the trailing ";" in the match to be even more restrictive. irb(main):030:0> if %r{"((?:\\.|[^"\\])*)"\s*=\s*"((?:\\.|[^"\\])*)"\s*;} =~ text then p $1, $2 end "--- this is an escaped quote: \\\"" "--- this is a string ---" => ["--- this is an escaped quote: \\\"", "--- this is a string ---"] Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/