Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: regex for negating a sequence

by davidrw (Prior)
on Oct 03, 2006 at 18:10 UTC ( #576141=note: print w/replies, xml ) Need Help??

in reply to regex for negating a sequence

i think this will work for you:
Based upon this test code to try to match ab followed by 4 bytes w/no ab sequence:
perl -le 'print for map { /ab(?!ab)(.(?!ab)){2}.{2}/s ? $& : () } @AR +GV' abhjabdf abasdasd abdasdadsab ababblah OUTPUTS: abasda abdasd abblah
Update: Here's a commented version:
/ \x26\x67 # Starting sequence (let's call it AB) (?!\x26\x67) # Exclude double starting seqeuence, e.g. ABAB (?: # non-capture .(?!\x26\x67) # Any character that's not followed by the sequenc +e ){6} # Need N-2 of these .{2} # And the last two characters can be anything. # note we know they're not AB because otherwise the # (N-2)th item in the previous part wouldn't have mat +ched /sx # Enable comments, and make . mean everything
Update: Here's a generic version:
my $marker = '&g'; my $N = 8; my $n1 = $N-2; /$marker(?!$marker)(?:.(?!$marker)){$n1}.{2}/s
Update: (As pointed out by diotalevi) Added the /s modifier in case the data stream can have a newline in it.

Replies are listed 'Best First'.
Re^2: regex for negating a sequence
by ikegami (Pope) on Oct 03, 2006 at 19:35 UTC

    Your regexp can be simplified to the following:


    If you'd rather have simplicity at the cost of an extra check, use the following:


    is the equivalent of
    but it (negatively) matches entire regexps instead of a choice of characters.

      Another alternative:
      I've interpreted "does not appear" differently than you when the eighth char is the \x26 of a \x26\x67 sequence.
      nice. My first attempt was trying to do it like that, but i started with something like:
      which doesn't work for the case of consectutive sequences.. i just didn't think of putting the look-ahead behind the dot :)
Re^2: regex for negating a sequence
by diotalevi (Canon) on Oct 03, 2006 at 18:36 UTC

    Your . is excluding the \n character. Use the /s flag in some form if you really mean "any character."

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://576141]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2020-08-12 15:52 GMT
Find Nodes?
    Voting Booth?
    Which rocket would you take to Mars?

    Results (66 votes). Check out past polls.