Perl6 Denial of multiple words and permutations of their characters inside a regular expression

What is the best way to accomplish, within a regular expression, the negation of several words and the permutations of the characters that make up these words?

For example: I do not want

"zero dollar"
"roze dollar"
"eroz dollar"
"one dollar"
"noe dollar"
"oen dollar"

but i really want

"thousand dollar"
"million dollar"
"trillion dollar"

If i write

not m/ [one | zero] \s dollar /

it will not correspond to permutations of characters, and the external “not” function will force the regular expression to match everything else, like a “big hit” without a “dollar” in the regular expression.

m/ <- [one] | [zero] > \s dollar/ # this is syntax error.

Many thanks!

lisprog

+4
source share
2 answers

Using code statement:

, <!{ }> , "" "":

say "two dollar" ~~ / :s ^ (\w+) <!{ $0.comb.sort.join eq "eno" | "eorz" }> dollar $ /;

before/after:

<!before > <!after > :

my @disallowed = <one zero>.map(|*.comb.permutations)».join.unique;

say "two dollar" ~~ / :s ^ <!before @disallowed>\w+ dollar $ /;
say "two dollar" ~~ / :s ^ \w+<!after @disallowed> dollar $ /;
+7

. is-bad-word, $needle (.. , ), @badwords any, True.

, (\w+), .

: (\w+) ( ), , ( , a dollar). , @badwords, ero .

, !

my @badwords = <one zero yellow>;

my @parsefails = q:to/EOF/.lines;
    zero dollar
    roze dollar
    erzo dollar
    one dollar
    noe dollar
    oen dollar
    yellow dollar
    wolley dollar
    EOF

my @parsepasses = q:to/EOF/.lines;
    thousand dollar
    million dollar
    dog dollar
    top dollar
    meme dollar
    EOF

sub is-bad-word($needle) {
    return $needle.comb.sort eq any(@badwords).comb.sort
}

use Test;
plan @parsefails + @parsepasses;

for flat (@parsefails X False), (@parsepasses X True) -> $line, $should-pass {
    my $succ = so $line ~~ / ^ (\w+) \s <!{ is-bad-word($0.Str) }> 'dollar' /;
    ok $succ eqv $should-pass, "$line -> $should-pass";
}

done-testing;
+4

Source: https://habr.com/ru/post/1671168/


All Articles