in reply to validating a quoted string

As others have replied, simply counting double-quote characters with  tr/// and discriminating based on even/odd count looks like the best way.

But if just gotta have a regex solution, maybe something like this will serve. Note that this code needs Perl version 5.10+ for the  (*SKIP) (*FAIL) verbs; see Special Backtracking Control Verbs in perlre. (Note also that I use  \x22 extensively in place of  " because otherwise Windows command-line escaping becomes too weird.)

c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; for my $string ( '\"my\" \"dog\"', '\"my\" \"dog shepherd\"', 'my dog', 'my \"dog shepherd\"', '\"my \"dog\"', 'my \"dog shepherd', ) { my $paired = qr{ \x22 [^\x22]* (?: \\. [^\x22]*)* \x22 }xms; my $unpaired = qr{ \x22 [^\x22]* \z }xms; ;; my $orphan = $string =~ m{ $paired (*SKIP) (*FAIL) | $unpaired }xms +; print qq{'$string' }, $orphan ? 'INVALID' : 'valid'; } " '"my" "dog"' valid '"my" "dog shepherd"' valid 'my dog' valid 'my "dog shepherd"' valid '"my "dog"' INVALID 'my "dog shepherd' INVALID


Give a man a fish:  <%-{-{-{-<