I like to match permutations of 0-8 with each digits occurring once using all 8 digits.

Um... the way I learned it, "0-8" represents 9 digits. (1-8 would be 8 digits, as would 0-7.) Is there some digit between 0 and 8 that you intend to leave out, and if so, which one?

Anyway, if you had said "permutations of 0-8 with each digit occurring once, using all 9 digits", then I would understand that you are looking only for strings of nine digit characters, such that all nine characters are distinct, and none of them is the digit "9":

#!/usr/bin/perl use strict; while (<DATA>) { chomp; if ( length() == 9 and not ( /[^0-8]/ or /(.).*\1/ )) { print "$_\n"; } else { warn "rejected input: $_\n"; } } __DATA__ 01234 123456780 1234567890 223456781 345678012 234567890 a12345678 012345678 456782011 456782013
Update: I suspect that the regex used as the last stage in my conditional is a fairly expensive operation; for strings that actually meet the criteria (are not rejected), it has to do 8+7+6+...+1 (total of 36) character comparisons to finish. (There should be some sort of "O(...n...)" expression for that, but it escapes me.) So, it would most likely be better to use a split/hash solution, as suggested by others, especially if you'll be handling large quantities of input with a relatively high "success" rate. Something like this:
while (<DATA>) { chomp; if ( length() == 9 and not /[^0-8]/ ) { my %c = map { $_ => undef } split //; if ( keys %c == 9 ) { print "$_\n"; next; } } warn "rejected input: $_\n"; }

In reply to Re^3: RegEx Question by graff
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.