in reply to Pattern matching when there are exception strings

I think what I would do is build a list of the prefixes and suffixes around the exclusions of 'ALPHA' -- in your case, @prefixes would be ("_", "#", and "CAW5 ") and @suffixes would be (). Then I'd produce a regex from those:
my (@pre, @suf); for (@exclusions) { my ($p, $s) = split /ALPHA/, $_, 2; push @pre, $p if length $p; push @suf, $s if length $s; } my $pre_rx = join "", map "(?<!\Q$_\E)", @pre; my $suf_rx = join "", map "(?!\Q$_\E)", @suf; my $total_rx = qr/${pre_rx}ALPHA$suf_rx/;
Now you can match your strings against the pattern in $total_rx.

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Replies are listed 'Best First'.
Re^2: Pattern matching when there are exception strings
by tomazos (Deacon) on Sep 21, 2005 at 14:16 UTC
    Excuse me for being thick but suppose you had the exceptions '1ALPHA2' and '3ALPHA4'. Wouldn't $total_rx also exclude '3ALPHA2' and '1ALPHA4'? :) Or am I missing something?

    Having said that I still think that lookahead and lookbehind are the best bet.

    Just join them like this instead:

    $total_raw = "(" . # WRONG join ("|", map ( "(?<!\Q$pre[$_]\E)ALPHA(?!\Q$suf[$_]\E)", (0..$#pre) ) ) . ")"; $total_rx = qr/$total_raw/;

    (untested) Or something like that.

    Update: Dooh. That doesn't work. Need an "and" match not an "or" match. Hmmmm... back to the drawing board.

    Update: Okay, I've got it...

    Can you nest lookaheads and lookbehinds? If so this should work:

    $total_raw = "(?!(" . # CORRECT, MAYBE join ("|", map ( "(?<=\Q$pre[$_]\E)ALPHA(?=\Q$suf[$_]\E)", (0..$#pre) ) ) . "))ALPHA"; $total_rx = qr/$total_raw/;

    This has a zero-width assertion followed by ALPHA.

    The zero-width assertion is a lookahead exclude (ie looking ahead, that this is not true)

    What it is looking ahead to see is one of multiple patterns.

    Each pattern contains an ALPHA with a lookahead and behind for an excluded suffix and prefix pair. If one of these matches, we have an excluded alpha - so we exclude this match (the original zero-width assertion).

    So it says: match an ALPHA that does not have an excluded prefix/suffix.

    -Andrew.


    Andrew Tomazos  |  andrew@tomazos.com  |  www.tomazos.com
      Ah, right. Very good point. I wasn't thinking straight.

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart