On Sat, Oct 6, 2012 at 1:21 AM, <[email protected]> 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/
-- You received this message because you are subscribed to the Google Groups
ruby-talk-google group. To post to this group, send email to
[email protected]. To unsubscribe from this group, send email
to [email protected]. For more options, visit this
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en