Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Match something that does not match

by jo37 (Scribe)
on Feb 15, 2020 at 17:42 UTC ( #11112994=perlquestion: print w/replies, xml ) Need Help??

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 (Chancellor) 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".

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11112994]
Approved by LanX
Front-paged by LanX
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2020-04-05 02:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The most amusing oxymoron is:
















    Results (33 votes). Check out past polls.

    Notices?