Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: regex for negating a sequence

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


in reply to regex for negating a sequence

i think this will work for you:
/\x26\x67(?!\x26\x67)(.(?!\x26\x67)){6}.{2}/s
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 (Patriarch) on Oct 03, 2006 at 19:35 UTC

    Your regexp can be simplified to the following:

    /\x26\x67(?:(?!\x26\x67).){7}./s

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

    /\x26\x67(?:(?!\x26\x67).){8}/s

    /(?:(?!$re).){8}/
    is the equivalent of
    /[^$chars]{8}/
    but it (negatively) matches entire regexps instead of a choice of characters.

      Another alternative:
      /\x26\x67(?!.{0,6}\x26\x67).{8}/s
      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:
      /\x26\x67(.(?!\x26\x67)){7}./s
      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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-24 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found