http://qs321.pair.com?node_id=11112994

jo37 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

every now and then I stumble upon the question of how to match something that doesn't match something else. I.e. some expression in the sense of [^...]* for a general given regex $match. The best I got so far is:

my $does_not_match = qr{((?:.*?(?=$match))|(?:(?:.(?!$match))*))};

The first branch matches a substring up to the given regex $match if there is a match and the second branch matches the whole string if there is no match. Both fail in the opposite case. (The second branch by missing the last character.)

Does anybody know something simpler? Or do you see any issues with the given regex?

Here is a small example:

#!/usr/bin/perl use Test2::V0; sub do_not_match { my $pat = shift; return qr{(?:.*?(?=$pat))|(?:(?:.(?!$pat))*)}; } my $re = do_not_match(qr{\b[aeiou][a-z]*ion\b}); is [/($re)/], ['stimulated '], 'matches prefix' for 'stimulated emission of radiation'; is [/($re)/], ['electron transition'],'no match' for 'electron transition'; is [/($re)/], [''], 'matches empty prefix' for 'absorbtion of photons'; is [/($re)/], ['light '], 'matches not greedy' for 'light amplification by stimulated emission of radiation'; is [/($re)\bimpact/], ['electron '], 'gives characters back' for 'electron impact ionization'; done_testing;

I might put this into an extension module for Regexp::Common, but I'm not sure if this makes sense at all. And what would a proper naming be? Maybe something like:

use Regexp::Common 'do_not_match'; my $re = $RE{do_not}{-match => 'something'}

Your opinions?

-jo

Replies are listed 'Best First'.
Re: Match something that does not match
by haukex (Archbishop) on Feb 15, 2020 at 20:13 UTC

    It seems to me like the feature you're implementing could be described as "stop matching at" (Update: or "match until") rather than do_not_match. Your current regex in do_not_match can be simplified to qr{ (?: (?!$pat) . )* }x; at least that still passes all your test cases. (This regex is pretty simple; I'm not sure if it warrants inclusion in Regexp::Common.)

    Update: WebPerl Regex Tester

      Your regex is indeed the one I was after. Though it works, I still don't get it :-(
      And I agree with you: this is too simple for Regexp::Common

      Thanks for your comment. Still learning...

      -jo

        Though it works, I still don't get it :-(

        The . matches anything (except newline), and (?!$pat) means "the next thing can't be $pat", so taken all together it really just means "match any number of characters, where none of these characters may be the first character of $pat".