I wouldn't use strings as a card representation. I'd either use bits (6 bits/card will do), or 2-element arrays (rank, suit).

Testing for pairs is easy: see if there are two card with the same rank (considering that in most poker forms, a hand has 5 or 7 cards, you don't have to be smart, even a brute force algorithm runs fast enough on anything produced since Babbage). You could, for instance, use a hash for ranks, and a hash for suits. If the ranks values are "2, 1, 1, 1", it's a pair. If it's "2, 2, 1", it's two pair. If it's "3, 1, 1", it's three of a kind. It it's "3, 2", it's a full house. If it's "4, 1", it's four of a kind. If it's "5", it's five of a kind. For the suits, if all the suits are the same (easy to find out with a hash), you have a flush. To find out straights, sort the cards (by rank). If the result is a sequence, you have a straight (an ace high straight needs an extra check).

Instead of hashes, you can sort right away, and determine pairs, etc, by inspecting the sorted hand.

But regexes seems like the wrong way to do it. In fact, if you need a regex to inspect an internal representation 100 out of 99 times, you're doing it wrong.


In reply to Re: Checking for a special matching by JavaFan
in thread Checking for a special matching by heatblazer

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.