[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


In reply to Re^3: RegEx Question by CountZero
in thread RegEx Question by yoda54

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.