2

我有一个类似的字符串{ASK(Value, Value, 'Sentence', Some_Char)},我需要在(). 我做错了什么?

preg_match_all('/\{ASK\((.*?),\)\}/', '{ASK(Value, Value, \'Sentence\', X)}', $matches);
print_r($matches); 
4

5 回答 5

0

从正则表达式中取出逗号,它匹配。

preg_match_all('/\{ASK\((.*?)\)\}/', '{ASK(Value, Value, \'Sentence\', X)}', $matches);
print_r($matches);

//Explode the matched group
$exploded = explode(',',$matches[1]); 
print_r($exploded);

/*
 * Note that we used $matches[1] instead of $matches[0], 
 * since the first element contains the entire matched 
 * expression, and each subsequent element contains the matching groups.
 */
于 2013-02-28T07:34:16.150 回答
0
$s = "{ASK(Value, Value, 'Sentence', Some_Char)}";
$p = '#\{ASK\((.*?)\)\}#';
preg_match_all($p, $s, $matches);
print_r($matches);
于 2013-02-28T07:36:42.903 回答
0

简单的分裂和爆炸

$Myval = "{ASK(Value, Value, 'Sentence', Some_Char)}";

$splitedVal = split('[()]', $Myval);

$explodedVal = explode(",", $splitedVal[1]);

print_r($explodedVal);

// 输出

Array ( [0] => Value [1] => Value [2] => 'Sentence' [3] => Some_Char ) 
于 2013-02-28T07:36:48.390 回答
0

一个简单的方法(虽然不完全包含在正则表达式中)可能是:

preg_match_all('/\{ASK\([^)]*\)\}/', '{ASK(Value, Value, \'Sentence\', X)}', $matches);
$values = explode($matches[1]);
于 2013-02-28T07:39:39.873 回答
0

只要您的Values,SentencesChars不包含,or ),那么这个单一的正则表达式模式将在没有额外explode()调用的情况下交付。

图案:(~(?:\G, |ASK\()\K[^,)]+~ 图案演示

代码:(演示

$string="{ASK(Value, Value, 'Sentence', Some_Char)}";

print_r(preg_match_all('~(?:\G, |ASK\()\K[^,)]+~',$string,$out)?$out[0]:[]);

输出:

Array
(
    [0] => Value
    [1] => Value
    [2] => 'Sentence'
    [3] => Some_Char
)

“魔法”在\G. 这告诉正则表达式在字符串的开头或在前一个匹配之后继续匹配。这是我发布的类似答案:https ://stackoverflow.com/a/48373347/2943403

于 2018-01-25T03:41:26.433 回答