I think it's not so bad doing it with a regexp if you're generating it programmatically. And dealing with it via split is easy when all letters must be distinct, but also gets more complex when you have to honour the template.

I think the direct version for the given template would look something like the below, which doesn't look too hard to generate programmatically either for this template or for other templates more generally:

m{ (.) (?!\1) (.) (?!\1|\2) (.) (?!\1|\2|\3) (.) (?!\1|\2|\3|\4) (.) (?!\1|\2|\3|\4|\5) (.) \1 }x

For your update version, thanks - I've not noticed before that we have \g-1 (though I think I'd want to add the optional braces - it doesn't look remotely atomic to me without them). But I believe your example is also for "all distinct letters", it isn't clear to me how you'd use it for BernieC's templates.

For performance: I'm assuming this is for matching something like English words in a dictionary, so the length is unlikely to be a problem - I'd look for a different approach if the templates were commonly going to have more than 10-20 distinct letters in them. There's a lot gained by the fact that matching a regexp is a single perl op: a split-based solution is likely to lose a lot more on the op overhead than it gains on the algorithmic complexity, if the target really is something like dictionary words.


In reply to Re^2: Nonrepeating characters in an RE (updated) by hv
in thread Nonrepeating characters in an RE by BernieC

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.