将一个字符串中的通用模式与另一个字符串匹配

我有一个以这种格式存储在数组中的扑克手列表:


$hands = ['AsQdTc9h', 'AsQsTd9d' ...]


我还有一个 4 个字符并使用字母的搜索字符串xyzw。例如,搜索字符串可能如下所示:


$searchString = 'xyxy';

$searchString = 'xyzw';

$searchString = 'yzyw';

搜索字符串的全部目的是为手中的小写字符识别所需的模式。


因此,如果 searchString 是xyzw,那只是一种说“仅选择没有小写字母相等的手”的方式。如果搜索字符串是xyxy,则表示“仅选择第一个和第三个小写字母相等且第二个和第四个小写字母相等的手”。


换句话说,小写字母必须与搜索字符串的模式匹配。如何在 PHP 中实现它?


阿晨1998
浏览 144回答 3
3回答

湖上湖

虽然这看起来像是一个模式匹配任务,但由于字符串的长度非常有限,伪暴力检查可能是最简单的。function pokerMatch(string $hand, string $pattern): bool{&nbsp; &nbsp; $hand = preg_replace('/[^a-z]/', '', $hand);&nbsp; &nbsp; for ($i = 0; $i < strlen($hand); $i++) {&nbsp; &nbsp; &nbsp; &nbsp; for ($j = $i+1; $j < strlen($hand); $j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($pattern[$i] === $pattern[$j] && $hand[$i] !== $hand[$j]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($pattern[$i] !== $pattern[$j] && $hand[$i] === $hand[$j]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return true;}基本上这是做什么的,它遍历模式字符串,获取每对字符并检查:如果模式位置 i 和 j 中的字母相等,则手串中的字符也必须相等;如果模式位置 i 和 j 中的字母不同,则手串中的字符也必须不同;如果其中任何一个不成立 - 模式不匹配如果在检查所有对后我们没有发现不匹配 - 那么它是匹配的。用法:var_dump(pokerMatch('AsQdTc9h', 'xyzw')); // => bool(true)var_dump(pokerMatch('AsQdTc9h', 'xyzz')); // => bool(false)var_dump(pokerMatch('AsQsTc9c', 'xxyy')); // => bool(true)var_dump(pokerMatch('AsQsTc9c', 'zzww')); // => bool(true) (it's agnostic to exact letters)

月关宝盒

$MyString = 'AsQdTc9h';$MyString = preg_replace('/[^a-z]/', '', $MyString); // Get only the lowercase characters// $MyString : sdch$LetterArray = str_split($MyString);$LetterArray = array_count_values($LetterArray);$ReplaceList = ['x', 'y', 'z', 'w'];$i = 0;foreach ($LetterArray as $Letter => $result) {&nbsp; &nbsp; $MyString = str_replace($Letter, $ReplaceList[$i], $MyString);&nbsp; &nbsp; $i++;}echo $MyString; // expected output : xyzw我想解释一下代码以便您理解它,首先我们使用正则表达式获取所有小写字符。然后我们将 4 个字符的单词转换为一个数组,然后我们计算有多少个字符是相同的。然后我们将结果替换为 xyzw 。

小唯快跑啊

您可以通过遍历每手牌和搜索字符串来解决此问题,记录哪个花色与哪个搜索字母匹配,并检查它们是否始终一致:$hands = ['AsQdTc9h', 'AsQsTd9d', 'AsKh9s9d'];$searchStrings = ['xyxy', 'xyzw', 'yzyw', 'ppqq'];function match_hand($hand, $search) {&nbsp; &nbsp; $h = preg_replace('/[^a-z]/', '', $hand);&nbsp; &nbsp; $matches = array();&nbsp; &nbsp; for ($i = 0; $i < strlen($search); $i++) {&nbsp; &nbsp; &nbsp; &nbsp; $s = $search[$i];&nbsp; &nbsp; &nbsp; &nbsp; // have we seen this search letter before?&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (isset($matches[$s])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // does it match the previous value? if not, it's an error&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($matches[$s] != $h[$i]) return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // haven't seen this search letter before, so this hand letter should not be in the matches array yet&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (in_array($h[$i], $matches)) return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $matches[$s] = $h[$i];&nbsp; &nbsp; }&nbsp; &nbsp; return true;}foreach ($hands as $hand) {&nbsp; &nbsp; foreach ($searchStrings as $search) {&nbsp; &nbsp; &nbsp; &nbsp; if (match_hand($hand, $search)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "hand $hand matches pattern $search\n";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}输出:hand AsQdTc9h matches pattern xyzwhand AsQsTd9d matches pattern ppqqhand AsKh9s9d matches pattern yzyw
打开App,查看更多内容
随时随地看视频慕课网APP