Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to calculate straight poker hand - Using ASCII CODE

Tags:

regex

poker

In another question I learned how to calculate straight poker hand using regex (here).

Now, by curiosity, the question is: can I use regex to calculate the same thing, using ASCII CODE?

Something like:

regex: [C][C+1][C+2][C+3][C+4], being C the ASCII CODE (or like this)

Matches: 45678, 23456

Doesn't matches: 45679 or 23459 (not in sequence)

like image 677
Topera Avatar asked Dec 17 '22 22:12

Topera


1 Answers

Your main problem is really going to be that you're not using ASCII-consecutive encodings for your hands, you're using numerics for non-face cards, and non-consecutive, non-ordered characters for face cards.

You need to detect, at the start of the strings, 2345A, 23456, 34567, ..., 6789T, 789TJ, 89TJQ, 9TJQK and TJQKA.

These are not consecutive ASCII codes and, even if they were, you would run into problems since both A2345 and TJQKA are valid and you won't get A being both less than and greater than the other characters in the same character set.

If it has to be done by a regex, then the following regex segment:

(2345A|23456|34567|45678|56789|6789T|789TJ|89TJQ|9TJQK|TJQKA)

is probably the easiest and most readable one you'll get.

like image 183
paxdiablo Avatar answered Apr 02 '23 12:04

paxdiablo