This is an addendum to my reply above, and fixes an error therein ((?R) vice (?-1)). It also avoids an s///e executable substitution.
Try this for better results (still needs 5.10+):
Output:use 5.010; use warnings; use strict; use Test::More # tests => ?? + 1 # Test::NoWarnings adds 1 test 'no_plan' ; use Test::NoWarnings; use constant TEST_SET => ( [ q{()}, q{()} ], [ q{()'}, q{!()} ], [ q{(()')'}, q{!(!())} ], [ q{((()')')'}, q{!(!(!()))} ], [ q{(()' (()')')'}, q{!(!() !(!()))} ], [ q{(A & B)}, q{(A & B)} ], [ q{(A & B)'}, q{!(A & B)} ], [ q{(A & B | (C & D)')'}, q{!(A & B | !(C & D))} ], [ q{((A & B)' | (A & C & (A & B & D)'))}, q{(!(A & B) | (A & C & !(A & B & D)))} ], [ q{((A | B)' & (C | (D & (E & F)))')'}, q{!(!(A | B) & !(C | (D & (E & F))))} ], [ q{((A & B)' | (A & C & (A & Boff' & D | (A & (B' & D)')'))}, q{(!(A & B) | (A & C & (A & !Boff & D | !(A & !(!B & D))))} ], ); FUNT: for my $func_name (qw(xform_4)) { note "\n----- testing $func_name() ----- \n\n"; *xform = do { no strict 'refs'; *$func_name; }; VECTOR: for my $ar_vector (TEST_SET) { if (not ref $ar_vector) { note $ar_vector; next VECTOR; } my ($string, $expected) = @$ar_vector; is xform($string), $expected, qq{$string} } # end for VECTOR } # end for FUNT done_testing; # functions under test ############################################# sub xform_4 { # needs 5.10+ for (?PARNO) and (?|...) my ($str, ) = @_; my $parenthetic = qr{ ( [(] (?: [^()]*+ | (?-1))* [)] ) }xmso; my $term = qr{ ( [[:alpha:]] \w* ) }xmso; 1 while $str =~ s{ (?| $parenthetic | $term) ' } {!$1}xmsgo; return $str; }
c:\@Work\Perl\monks\Learning_Perl_2017>perl xform_nested_terms_1.pl # # ----- testing xform_4() ----- # ok 1 - () ok 2 - ()' ok 3 - (()')' ok 4 - ((()')')' ok 5 - (()' (()')')' ok 6 - (A & B) ok 7 - (A & B)' ok 8 - (A & B | (C & D)')' ok 9 - ((A & B)' | (A & C & (A & B & D)')) ok 10 - ((A | B)' & (C | (D & (E & F)))')' ok 11 - ((A & B)' | (A & C & (A & Boff' & D | (A & (B' & D)')')) 1..11 ok 12 - no warnings 1..12
Give a man a fish: <%-{-{-{-<
In reply to Re: Still having trouble with regexes! Please help
by AnomalousMonk
in thread Still having trouble with regexes! Please help
by Learning_Perl_2017
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |