重复捕获组遇到问题:
(?:\[)(?:(?:\s*?)([2-9AQTKJ][shcd])+?(?:\s*?)).*?(?:\])
请参阅demo
基本上我想匹配方括号内的卡片值(As
,8h
等..)组合
非常感谢任何帮助。
由于
答案 0 :(得分:3)
您可以使用带有\G
运算符的正则表达式匹配[...]
内的多个子字符串:
(?:\[|(?!^)\G)\s*\K[2-9AQTKJ][shcd](?=[^\]]*])
请参阅regex demo
简而言之,此PCRE正则表达式将匹配以下文本:
(?:\[|(?!^)\G)\s*\K
- 从[
开始,或者在上一次成功匹配结束时跟随零个或多个空白符号[2-9AQTKJ][shcd]
- 每个已定义的集合匹配2个字符(?=[^\]]*])
- 一个积极的预测,检查当前位置之前是否有结束]
$re = '~(?:\[|(?!^)\G)\s*\K[2-9AQTKJ][shcd](?=[^\]]*])~';
$str = "[As 4h 8s] [ As 4h ] [As4h] As [ 4h ";
preg_match_all($re, $str, $matches);
print_r($matches[0]);
答案 1 :(得分:2)
这是否是你想要的?
/\[([^\]]+)\]/
https://regex101.com/r/vT8aC8/2
$str= "[As 4h 8s] [ As 4h ] [As4h] As [ 4h ";
preg_match_all("/\[([^\]]+)\]/", $str, $matches);
$values = $matches[1];
$result = [];
foreach($values as $value)
{
$parts = preg_split("/ /", $value, -1, PREG_SPLIT_NO_EMPTY);
foreach($parts as $part)
{
array_push($result, $part);
}
}
var_dump($result); // $result will contain all the values you want