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

[0-8]{1, 1} matches one digit out of the set {0 1 2 3 4 5 6 7 8}, so it doesn't do what you need at all.

Actually, I am not sure that you can do this in one regex.

I will show a simpler example only using permutations of the digits 0 - 3 (to make it easier to follow):

First you have to check that your string contains 4 digits in the range 0 - 3. That one is easy: /^[0-3]{4}$/

If your string passes this test, then you check whether each digit occurs only once.

/^(\d)(?!\d*\1)(\d)(?!\d*\2)(\d)(?!\d*\3)\d$/;
This works by capturing each digit and using negative look-aheads to check that this digit does not re-occur again in the string.

The following program proves that it works:

use warnings; use strict; for my $one (0 .. 3) { for my $two (0 .. 3) { for my $three (0 .. 3) { for my $four (0 .. 4) { my $test = "$one$two$three$four"; print "$test\n" if $test=~m/^[0-3]{4}$/ and $test=~m/^ +(\d)(?!\d*\1)(\d)(?!\d*\2)(\d)(?!\d*\3)\d$/; } } } }
Output:
0123 0132 0213 0231 0312 0321 1023 1032 1203 1230 1302 1320 2013 2031 2103 2130 2301 2310 3012 3021 3102 3120 3201 3210

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^4: RegEx Question
by ikegami (Patriarch) on Sep 10, 2009 at 00:46 UTC

    Actually, I am not sure that you can do this in one regex.

    Assuming he meant all 9 digits,

    / ^ (?=[0-8]{9}\z) (?=.*0) (?=.*1) (?=.*2) (?=.*3) (?=.*4) (?=.*5) (?=.*6) (?=.*7) (?=.*8) /sx;
      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

        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/
Re^4: RegEx Question
by yoda54 (Monk) on Sep 09, 2009 at 21:34 UTC
    Thanks for the reply... I was really hoping for a easy way out...!! :)

      It depends a great deal on what you consider to be 'easy' and probably also it depends what it is that you actually want to achieve. The following code may help:

      use strict; use warnings; for my $number (1233456, 10, 1, 555789041) { my %digits; my @failDigits = grep {++$digits{$_} == 2} split '', $number; next if ! @failDigits; print "Number $number failed for:\n"; print " $_: $digits{$_} places\n" for @failDigits; }

      Prints:

      Number 1233456 failed for: 3: 2 places Number 555789041 failed for: 5: 3 places

      True laziness is hard work