Having trouble with a repeated capturing group:
(?:\[)(?:(?:\s*?)([2-9AQTKJ][shcd])+?(?:\s*?)).*?(?:\])
See demo
Basically I want to match the card value ( As
, 8h
etc..) combinations inside the square brackets only
Any help would be greatly appreciated.
Thanks
You can use a regex with a \\G
operator to match multiple substrings inside [...]
:
(?:\[|(?!^)\G)\s*\K[2-9AQTKJ][shcd](?=[^\]]*])
See regex demo
In short, this PCRE regex will match a text that:
(?:\\[|(?!^)\\G)\\s*\\K
- starts with [
or is at the end of the previous successful match followed with zero or more whitespace symbols [2-9AQTKJ][shcd]
- matches 2 characters each of the defined sets (?=[^\\]]*])
- a positive lookahead checking if there is a closing ]
ahead of the current position PHP demo :
$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]);
Is this want you want?
/\\[([^\\]]+)\\]/
https://regex101.com/r/vT8aC8/2
EDIT ( Full PHP Solution )
$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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.