If you can construct your line a little differently, you can make your regex a whole lot uglier by using negative look-behind assertions (perlre):
I also removed the superfluous sprintf's.use strict; use warnings; my $line = qq{Test #1 'Show me Waynes world','Jennys Basketball shoes' +\n}; $line =~ s/(?<!\\)(['"]).+?(?<!\\)(['"])/$1SSS$2/g; print $line; $line = q{Test #2 'Show me Wayne\'s world','Jenny\'s Basketball shoes' +} . "\n"; $line =~ s/(?<!\\)(['"]).+?(?<!\\)(['"])/$1SSS$2/g; print $line; __END__ Test #1 'SSS','SSS' Test #2 'SSS','SSS'
Update: YAPE::Regex::Explain to the rescue:
use YAPE::Regex::Explain; my $re = q{(?<!\\)(['"]).+?(?<!\\)(['"]}; print YAPE::Regex::Explain->new($re)->explain(); __END__ The regular expression: (?-imsx:(?<!\)(['"]).+?(?<!\)(['"])))) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- (?<! look behind to see if there is not: ---------------------------------------------------------------------- \) ')' ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- ['"] any character of: ''', '"' ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- .+? any character except \n (1 or more times (matching the least amount possible)) ---------------------------------------------------------------------- (?<! look behind to see if there is not: ---------------------------------------------------------------------- \) ')' ---------------------------------------------------------------------- ( group and capture to \2: ---------------------------------------------------------------------- ['"] any character of: ''', '"' ---------------------------------------------------------------------- ) end of \2 ---------------------------------------------------------------------- ) end of look-behind ---------------------------------------------------------------------- ) end of look-behind ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
In reply to Re: skip over an escaped single quote
by toolic
in thread skip over an escaped single quote
by bplegend
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |