in reply to [CLOSED] RegExp: words excepstions list similar to characters' one.

Hello nikolay,

I would store the exceptions in two hashes, then use exists to test whether a given word should be excluded. I begin by splitting the input string on whitespace. Note the presence of a capture group in the regular expression given to split: the captured string (in this case, the whitespace) is added to the list returned by split, to enable the string to be reassembled correctly after the substitutions have been performed.

#! perl use strict; use warnings; my %exclude_left = map { lc $_ => undef } qw( web 2 ); my %exclude_right = map { lc $_ => undef } qw( program call ); my $z = 'Web-developer, perl-program, explicit-element, function-call, + 2-x speed.'; my @phrases = split /(\s+)/, $z; for (@phrases) { # 1 2 3 <-- capture groups if (/ ( (\w+) - (\w+) ) /x) { my ($phrase, $left, $right) = ($1, $2, $3); s/$phrase/$right-$left/ unless exists $exclude_left {lc $left +} || exists $exclude_right{lc $right +}; } } print '|', join('', @phrases), "|\n";

Output:

17:36 >perl 1667_SoPW.pl |Web-developer, perl-program, element-explicit, function-call, 2-x spe +ed.| 17:36 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: RegExp: words exceptions list similar to characters' one.
by AnomalousMonk (Archbishop) on Jun 29, 2016 at 18:12 UTC

    I like the hash-lookup approach, but nikolay seems to want to exclude digit groups rather than just the explicit  "2" substring. (I admit this is a bit difficult to divine given the pseudo-regex provided in the OP.) So, a substring like  "23-x" in the input string would become  "x-23" in the output string. Again, my guess is that this is not what the OPer wants.


    Give a man a fish:  <%-{-{-{-<

      Ah-ha. Insufficient test cases :(