I like it:
perl -ne 'print "$_\n" foreach /"((?>[^"\\]|\\+[^"]|\\(?:\\\\)*")*)"/g;'
This is a bit verbose, but it handles escaped quotes and discards much better than a simple implementation. What he says:
my $re = qr{
" # Begin it with literal quote
(
(?> # prevent backtracking once the alternation has been
# satisfied. It either agrees or it does not. This expression
# only needs one direction, or we fail out of the branch
[^"\\]
| \\+ # OR if a backslash, then any number of backslashes followed by
[^"] # something that is not a quote
| \\
(?>\\\\)*
" # and a quote
)* # any number of *set* qualifying phrases
) # all batched up together
"
}x;
If you don’t need such power - say that these are most likely dialogs, not structured quotes, then
/"([^"]*)"/
probably works just like everything else.
source
share