(?>[^$suits]) is the same as [^$suits]. The + should be inside the (?>).

Captures are slow, so it would be faster to build a regex without captures I also generalised some of the matching. Input validation is not a goal here.

m{ ^ . (?: D (?: . .D ){4} | S (?: . .S ){4} | C (?: . .C ){4} | H (?: . .H ){4} ) \z }xs;

Or maybe even

m{ ^ . (?: D..D..D..D..D | S..S..S..S..S | C..C..C..C..C | H..H..H..H..H ) \z }xs;
built it manually here, but it can be built dynamically.

Update: That last regex pattern gives me an idea:

my $masked = $_ & "\x00\xFF \x00\xFF \x00\xFF \x00\xFF \x00\xFF"; ( $masked eq "\x00D \x00D \x00D \x00D \x00D" || $masked eq "\x00S \x00S \x00S \x00S \x00S" || $masked eq "\x00C \x00C \x00C \x00C \x00C" || $masked eq "\x00H \x00H \x00H \x00H \x00H" )

Of course, if the input format was a number, it would be even faster:

sub build { return ( $_[0] << (16*4 + 2*5 ) ) + ( $_[1] << (16*4 + 2*4 ) ) + ( $_[2] << (16*3 + 2*4 ) ) + ( $_[3] << (16*3 + 2*3 ) ) + ( $_[4] << (16*2 + 2*3 ) ) + ( $_[5] << (16*2 + 2*2 ) ) + ( $_[6] << (16*1 + 2*2 ) ) + ( $_[7] << (16*1 + 2*1 ) ) + ( $_[8] << (16*0 + 2*1 ) ) + ( $_[9] << (16*0 + 2*0 ) ); } use constant { SUITS_MASK => build(0,3, 0,3, 0,3, 0,3, 0,3), ALL_D => build(0,0, 0,0, 0,0, 0,0, 0,0), ALL_S => build(0,1, 0,1, 0,1, 0,1, 0,1), ALL_C => build(0,2, 0,2, 0,2, 0,2, 0,2), ALL_H => build(0,3, 0,3, 0,3, 0,3, 0,3), }; my $suits = $_ & SUITS_MASK; ( $suits == ALL_D || $suits == ALL_S || $suits == ALL_C || $suits == ALL_H )

In reply to Re^6: Simple Matching by ikegami
in thread Simple Matching by rnroot

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.