in reply to Re: Find duplicate digits
in thread Find duplicate digits

Hi japhy,

I'm trying to understand your updated regex:

m{ ^ (?= \d* (\d) \d* \1 ) (?! \d* \1 \d* \1 \d* \1 ) }x;
Could you explain what's going there? Tia :)

Replies are listed 'Best First'.
Re^3: Find duplicate digits
by japhy (Canon) on Feb 16, 2006 at 07:43 UTC
    The regex is anchored to the beginning of the string. The first assertion is a positive look-ahead that says "see if we can find zero or more digits, followed by a particular digit, followed by zero or more digits, and then that particular digit again". That satisfies the "at least one digit must be duplicated" rule. The second assertion is a negative look-ahead that says "make sure we can't match zero or more digits, that particular digit, zero or more digits, that particular digit, and then zero or more digits and that particular digit a THIRD time". This satisfies the "the digit can't appear more than three times" rule.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      Great thanks!

      That solves the mystery partially for me, because I'm always finding it hard to understand regex.