Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1001797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:43:57+00:00 2026-05-16T07:43:57+00:00

Is there a regex to calculate straight poker hand? I’m using strings to represent

  • 0

Is there a regex to calculate straight poker hand?

I’m using strings to represent the sorted cards, like:

AAAAK#sssss = 4 aces and a king, all of spades.
A2345#ddddd = straight flush, all of diamonds.

In Java, I’m using these regexes:

regexPair = Pattern.compile(".*(\\w)\\1.*#.*");
regexTwoPair = Pattern.compile(".*(\\w)\\1.*(\\w)\\2.*#.*");
regexThree = Pattern.compile(".*(\\w)\\1\\1.*#.*");
regexFour = Pattern.compile(".*(\\w)\\1{3}.*#.*");
regexFullHouse = Pattern.compile("((\\w)\\2\\2(\\w)\\3|(\\w)\\4(\\w)\\5\\5)#.*");
regexFlush = Pattern.compile(".*#(\\w)\\1{4}");

How to calculate straight (sequences) values with regex?

EDIT

I open another question to solve the same problem, but using ascii value of char,
to regex be short. Details here.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:43:58+00:00Added an answer on May 16, 2026 at 7:43 am

    I have to admit that regular expressions are not the first tool I would have thought of for doing this. I can pretty much guarantee that any RE capable of doing that to an unsorted hand is going to be far more hideous and far less readable than the equivalent procedural code.

    Assuming the cards are sorted by face value (and they seem to be otherwise your listed regexes wouldn’t work either), and you must use a regex, you could use a construct like

    2345A|23456|34567|...|9TJQK|TJQKA
    

    to detect the face value part of the hand.

    In fact, from what I gather here of the “standard” hands, the following should be checked in order of decreasing priority:

    Royal/straight flush: "(2345A|23456|34567|...|9TJQK|TJQKA)#(\\w)\\1{4}"
    Four of a kind:       ".*(\\w)\\1{3}.*#.*"
    Full house:           "((\\w)\\2\\2(\\w)\\3|(\\w)\\4(\\w)\\5\\5)#.*"
    Flush:                ".*#(\\w)\\1{4}"
    Straight:             "(2345A|23456|34567|...|9TJQK|TJQKA)#.*"
    Three of a kind:      ".*(\\w)\\1\\1.*#.*"
    Two pair:             ".*(\\w)\\1.*(\\w)\\2.*#.*"
    One pair:             ".*(\\w)\\1.*#.*"
    High card:            (none)
    

    Basically, those are the same as yours except I’ve added the royal/straight flush and the straight. Provided you check them in order, you should get the best score from the hand. There’s no regex for the high card since, at that point, it’s the only score you can have.

    I also changed the steel wheel (wrap-around) straights from A2345 to 2345A since they’ll be sorted that way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a string like Marty Mcfly is there a regex or other one line
I am parsing some HTML source. Is there a regex script to find out
There's many regex's out there to match a URL. However, I'm trying to match
I have noticed in the PHP regex library there is a choice between ereg
There is a method Regex.Replace(string source, string pattern, string replacement) The last parameter supports
Is there a way to get single regex to satisfy this condition?? I am
Is there a way to pass a variable into a regex in jQuery/Javascript? I
Is there a way in Ant to just use regex to search through a
i know there are several questions about regex recursion in .net. I can write
Is there any way to handle the exception if regex.Matches didn't match anything and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.