Learning_Perl_2017 has asked for the wisdom of the Perl Monks concerning the following question:
We have complicated expressions like this, which may also contain deep nesting:
((A & B)' | (A & C & (A & B & D)'));
Every time we see sub expressions like:
(1) (A & B)\' and
(2) (A & C & (A & B & D)')
we have to substitute them with the following:
!(A & B) for (1) above
and (A & C & !(A & B & D)) for (2) above.
I wrote the following but it obviously requires some more improvements. Can you help me know what I am doing wrong?
#!/usr/bin/perl use strict; use warnings; my $x = '((A & B)\' | (A & C & (A & B & D)\'))'; print "Given: $x\n"; $x =~ s/(\(.*?\))\'/\!$1/g; print "Calculated: ",$x,"\n";
That prints:
Given: ((A & B)' | (A & C & (A & B & D)')) Calculated: (!(A & B) | !(A & C & (A & B & D)))
The second part of the sub expression is incorrect as my regex match did not do what is intended, which is: substitute (A & B & D)' with !(A & C & (A & B & D))
Thank you friends.
Bill Murphy.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex matching and substitution
by choroba (Cardinal) on Jan 29, 2016 at 08:08 UTC | |
by BillKSmith (Monsignor) on Jan 29, 2016 at 14:56 UTC | |
|
Re: Regex matching and substitution
by kcott (Archbishop) on Jan 29, 2016 at 07:14 UTC | |
|
Re: Regex matching and substitution
by NetWallah (Canon) on Jan 29, 2016 at 06:35 UTC |