我希望这是一个简单的问题,但我仍然围绕着群体。
我有这个字符串:this is some text [propertyFromId:34] and this is more text
我会更喜欢他们。我需要在括号之间获取内容,然后在冒号左侧显示仅包含alpha的文本的组,并使用冒号右侧的整数组。
所以,完整匹配:propertyFromId:34
,第1组:propertyFromId
,第2组:34
这是我的出发点(?<=\[)(.*?)(?=])
答案 0 :(得分:0)
使用
\[([a-zA-Z]+):(\d+)]
请参阅regex demo
<强>详情:
\[
- [
符号([a-zA-Z]+)
- 第1组捕获一个或多个字母字符([[:alpha:]]+
或\p{L}+
也可以使用):
- 冒号(\d+)
- 第2组捕获一个或多个数字]
- 结束]
符号。$re = '~\[([a-zA-Z]+):(\d+)]~';
$str = 'this is some text [propertyFromId:34] and this is more text';
preg_match_all($re, $str, $matches);
print_r($matches);
// => Array
// (
// [0] => Array
// (
// [0] => [propertyFromId:34]
// )
//
// [1] => Array
// (
// [0] => propertyFromId
// )
//
// [2] => Array
// (
// [0] => 34
// )
//
// )