in reply to Re^4: RegEx Question
in thread RegEx Question

Very well done! A Regex which consists of nothing but an anchor and lookaheads. It takes true greatness to think of such a thing.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^6: RegEx Question
by ikegami (Patriarch) on Sep 10, 2009 at 06:32 UTC
    Well, whichever lookahead is placed last doesn't need to be a lookahead, and the order doesn't matter as far as the result is concerned, so I could have written the pattern as follows:
    / ^ (?=.*0) (?=.*1) (?=.*2) (?=.*3) (?=.*4) (?=.*5) (?=.*6) (?=.*7) (?=.*8) .{9}\z /sx;

    I figured it would be faster to check the length and for the presence of invalid characters first, and I left the last one as a lookahead for symmetry.

    Note that this is basically the trick for doing "and" in regex matches. My original solution is roughly the same as

    /^[0-8]{9}\z/ && /0/ && /1/ && /2/ && /3/ && /4/ && /5/ && /6/ && /7/ && /8/