my $balpar_re=qr{
(
\( # opening (
(?:[^'"()]++ # Not a ', ", ( or ) - no backtracking
# incorrect |(?:'(?:[^']|\')*?') #' a single quote string
# incorrect |(?:"(?:[^"]|\")*?") #" a double quote string
|(?:'(?:\\'|[^'])*?') #' a single quote string
|(?:"(?:\\"|[^"])*?") #" a double quote string
|(?-1) # must be a ( or ) so recurse
)*+ # zero or more time - no backtracking
\) # closing )
)
}x;
####
#!/usr/bin/env perl
use Data::Dumper;
use strict;
use warnings;
my $string=q{INSERT INTO sample_table (first_name,last_name) VALUES ('John','Doe'),('John','Smith'),('Foo','O\'Bar'),('Baz','D\'Qux (II)'),('(and so on, ','and so forth);');};
my $balpar_re=qr{
(
\( # opening (
(?:[^'"()]++ # Not a ', ", ( or ) - no backtracking
|(?:'(?:\\'|[^'])*?') #' a single quote string
|(?:"(?:\\"|[^"])*?") #" a double quote string
|(?-1) # must be a ( or ) so recurse
)*+ # zero or more time - no backtracking
\) # closing )
)
}x;
$string=~ s{$balpar_re}{mung($1)}ge;
exit;
sub mung {
warn Data::Dumper->Dump([\@_],[qw(*_)]),' ';
}
##
##
@_ = (
'(first_name,last_name)'
);
at perlmonks.pl line 25.
@_ = (
'(\'John\',\'Doe\')'
);
at perlmonks.pl line 25.
@_ = (
'(\'John\',\'Smith\')'
);
at perlmonks.pl line 25.
@_ = (
'(\'Foo\',\'O\\\'Bar\')'
);
at perlmonks.pl line 25.
@_ = (
'(\'Baz\',\'D\\\'Qux (II)\')'
);
at perlmonks.pl line 25.
@_ = (
'(\'(and so on, \',\'and so forth);\')'
);
at perlmonks.pl line 25.