Are you trying to find the corresponding left parenthesis? You don't need a regex, just use a stack of the positions of the left parentheses:
#!/usr/bin/perl use warnings; use strict; for my $given ( "(A & B)'", "((A & B)' | (A & C & (A & B & D)'))", "((A | B)' & (C | (D & (E & F)))')'", ) { my @left; my $calc = $given; my $last; for my $pos (0 .. length($given) - 1) { my $current = substr $given, $pos, 1; push @left, $pos if '(' eq $current; $last = pop @left if ')' eq $current; if ("'" eq $current) { substr $calc, $pos, 1, q(); substr $calc, $last, 0, '!'; } } print "Given: $given\n"; print "Calculated: $calc\n"; }

Update: Or, if you really need to use a regex, use a recursive subpattern (see perlre):

#!/usr/bin/perl use warnings; use strict; my $re = qr/(\(((?:(?>[^()]+)|(?1))*)\))'/; for my $given ( "(A & B)'", "((A & B)' | (A & C & (A & B & D)'))", "((A | B)' & (C | (D & (E & F)))')'", ) { my $calc = $given; $calc =~ s/$re/!($2)/g while $calc =~ /'/; print "Given: $given\n"; print "Calculated: $calc\n"; }

Update 2: You can also write a full parser for the expressions:

#!/usr/bin/perl use warnings; use strict; use Marpa::R2; my $dsl = << '__DSL__'; :default ::= action => concat lexeme default = latm => 1 Expression ::= '(' Expression ')' assoc => group | literal | Expression quote action => negation || Expression sp operator sp Expression quote ~ ['] # ' operator ~ [&|] literal ~ [A-Z] sp ~ [\s]+ __DSL__ my $i = 0; sub concat { $i++; join q(), @_[ 1 .. $#_ ] } sub negation { $i++; "!$_[1]" } my $grammar = 'Marpa::R2::Scanless::G'->new({ source => \$dsl }); for my $given ( "(A & B)'", "((A & B)' | (A & C & (A & B & D)'))", "((A | B)' & (C | (D & (E & F)))')'", ) { my $calc = $grammar->parse(\$given, { semantics_package => 'main' +}); print "Given: $given\n"; print "Calculated: $$calc\n"; }
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

In reply to Re: Regex matching and substitution by choroba
in thread Regex matching and substitution by Learning_Perl_2017

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.