Here's an approach that I think is more general, although I have my doubts about readability/maintainability. It seems much more verbose, but that's mainly due to the testing framework. (Tested under Perl version 5.8.9.)
File exclude_words_1.pl:
# pragmata ######################################################### use warnings; use strict; # modules ########################################################## use Test::More # tests => ?? + 1 # Test::NoWarnings adds 1 test 'no_plan' ; use Test::NoWarnings; use Data::Dump qw(dd); use constant DEBUG => 0; # prototypes ####################################################### sub prepare_excluded ($$); # prototyping might actually be useful her +e # main program ##################################################### my $sep = qr{ - }xms; # what's a word? this is a very naive definition (includes digits!). my $word = qr{ (?<! [[:alnum:]]) [[:alnum:]]+ (?! [[:alnum:]]) }xms; my $exclude_pre_word = prepare_excluded( # generic word, always first $word => [ # followed by freely mixed list of words and patterns to exclude # raw words 'web', # regex objects mixed in freely qr{ (?<! \d) \d+ (?! \d) }xms, ] ); DEBUG and print qq{pre: $exclude_pre_word \n\n}; my $exclude_post_word = prepare_excluded($word => [ qw(program call) +]); DEBUG and print qq{post: $exclude_post_word \n\n}; VECTOR: for my $ar_vector ( [ 'WeB-developer, perl-pRoGrAm, explicit-element, function-CaLl, 2 +3-x speed.', 'WeB-developer, perl-pRoGrAm, element-explicit, function-CaLl, 2 +3-x speed.', ], [ 'developer-Web, program-perl, explicit-element, call-function, x +-23 speed.', 'Web-developer, perl-program, element-explicit, function-call, 2 +3-x speed.', ], ) { if (not ref $ar_vector) { note $ar_vector; next VECTOR; } my ($string, $expected) = @$ar_vector; (my $got = $string) =~ s{ ($exclude_pre_word) ($sep) ($exclude_post_word) } {$3$2$1}xmsg; is $got, $expected, qq{'$string'}; } # for VECTOR note "null pre- and post-exclusion list (everything swapped)"; $exclude_pre_word = prepare_excluded($word => []); $exclude_post_word = prepare_excluded($word => []); VECTOR: for my $ar_vector ( [ 'WeB-developer, perl-pRoGrAm, explicit-element, function-CaLl, 2 +3-x speed.', 'developer-WeB, pRoGrAm-perl, element-explicit, CaLl-function, x +-23 speed.', ], [ 'developer-Web, program-perl, explicit-element, call-function, x +-23 speed.', 'Web-developer, perl-program, element-explicit, function-call, 2 +3-x speed.', ], ) { if (not ref $ar_vector) { note $ar_vector; next VECTOR; } my ($string, $expected) = @$ar_vector; (my $got = $string) =~ s{ ($exclude_pre_word) ($sep) ($exclude_post_word) } {$3$2$1}xmsg; is $got, $expected, qq{'$string'}; } # for VECTOR note "universal pre- and post-exclusion list (nothing swapped)"; $exclude_pre_word = prepare_excluded($word => [ $word ]); $exclude_post_word = prepare_excluded($word => [ $word ]); VECTOR: for my $ar_vector ( [ 'WeB-developer, perl-pRoGrAm, explicit-element, function-CaLl, 2 +3-x speed.', 'WeB-developer, perl-pRoGrAm, explicit-element, function-CaLl, 2 +3-x speed.', ], [ 'developer-Web, program-perl, explicit-element, call-function, x +-23 speed.', 'developer-Web, program-perl, explicit-element, call-function, x +-23 speed.', ], ) { if (not ref $ar_vector) { note $ar_vector; next VECTOR; } my ($string, $expected) = @$ar_vector; (my $got = $string) =~ s{ ($exclude_pre_word) ($sep) ($exclude_post_word) } {$3$2$1}xmsg; is $got, $expected, qq{'$string'}; } # for VECTOR done_testing; # subroutines ###################################################### sub prepare_excluded ($$) { # prototyping might actually be useful he +re my ($word, # generic acceptable word pattern $ar_exclusions, # array ref.: words/patterns to exclude ) = @_; # avoid (?!) case which always fails. return qr{ $word }xms unless @$ar_exclusions; my ($exclude) = map qr{ $_ }xms, join ' | ', map prepare_single_exclusion($_), @$ar_exclusions ; return qr{ (?! $exclude) $word }xms; } sub prepare_single_exclusion { my ($this, # regex pattern or word-like thing ) = @_; # already a regex object: just return it. return $this if 'Regexp' eq ref $this; # pure alpha: return guarded alpha sequence. return qr{ (?i) (?<! [[:alpha:]]) $this (?! [[:alpha:]]) }xms if $this !~ m{ [^[:alpha:]] }xms; # default: mixed alpha and non-alpha: just quotemeta it. return qr{ (?i) \Q$this\E }xms; }
Output:
c:\@Work\Perl\monks\nikolay>perl exclude_words_1.pl ok 1 - 'WeB-developer, perl-pRoGrAm, explicit-element, function-CaLl, +23-x speed.' ok 2 - 'developer-Web, program-perl, explicit-element, call-function, +x-23 speed.' # null pre- and post-exclusion list (everything swapped) ok 3 - 'WeB-developer, perl-pRoGrAm, explicit-element, function-CaLl, +23-x speed.' ok 4 - 'developer-Web, program-perl, explicit-element, call-function, +x-23 speed.' # universal pre- and post-exclusion list (nothing swapped) ok 5 - 'WeB-developer, perl-pRoGrAm, explicit-element, function-CaLl, +23-x speed.' ok 6 - 'developer-Web, program-perl, explicit-element, call-function, +x-23 speed.' 1..6 ok 7 - no warnings 1..7
Give a man a fish: <%-{-{-{-<
In reply to Re: RegExp: words excepstions list similar to characters' one.
by AnomalousMonk
in thread [CLOSED] RegExp: words excepstions list similar to characters' one.
by nikolay
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |