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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |