As I've shown you, there are other solutions than using regular expression to solve the problem. As it seems, your original question didn't involve the negation of a literal. It's easy to adapt the non-regex solutions to handle it, adapting the regex solution is left as an exercise for the reader.

Also note that your new expression is invalid: the number of left and right parentheses isn't the same. I added one more ) to the end.

Walking the string manually:

#!/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)))')'", "((A & B)' | (A & C & (A & B & D | (A & (B' & D)')'))" ) { 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(); if (')' eq substr $calc, $pos - 1, 1) { # Negated parenthe +ses. substr $calc, $last, 0, '!'; } else { # Negated literal. substr $calc, $pos - 1, 0, '!'; } } } print "Given: $given\n"; print "Calculated: $calc\n"; }

Writing a parser (no change required, I just changed the whitespace behaviour to make it even easier):

#!/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 operator Expression quote ~ ['] # ' operator ~ [&|] literal ~ [A-Z] :discard ~ sp sp ~ [\s]+ __DSL__ my $i = 0; sub concat { $i++; join ' ', @_[ 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)))')'", "((A & B)' | (A & C & (A & B & D | (A & (B' & D)')'))) +" ) { 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: Still having trouble with regexes! Please help by choroba
in thread Still having trouble with regexes! Please help 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.