【发布时间】:2014-02-10 08:43:58
【问题描述】:
我有字符串例如:
$stringExample = "(({FAPAGE15}+500)/{GOGA:V18})"
// separete content { }
我需要这样的结果::
$response = array("FAPAGE15","GOGA:V18")
我认为它必须是:preg_split 或 preg_match
【问题讨论】:
标签: regex
我有字符串例如:
$stringExample = "(({FAPAGE15}+500)/{GOGA:V18})"
// separete content { }
我需要这样的结果::
$response = array("FAPAGE15","GOGA:V18")
我认为它必须是:preg_split 或 preg_match
【问题讨论】:
标签: regex
这是您需要的正则表达式:
\{(.*?)\}
正则表达式示例:
PHP:
$str = "(({FAPAGE15}+500)/{GOGA:V18})";
preg_match_all("/\{(.*?)\}/", $str, $matches);
print_r($matches[1]);
输出:
Array
(
[0] => FAPAGE15
[1] => GOGA:V18
)
工作示例:
【讨论】:
您可以使用否定字符类:[^}] (所有不是})
preg_match_all('~(?<={)[^}]++(?=})~', $str, $matches);
$result = $matches[0];
模式细节
~ # pattern delimiter
(?<={) # preceded by {
[^}]++ # all that is not a } one or more times (possessive)
(?=}) # followed by }
~ # pattern delimiter
注意:所有格量词++ 不是获得好的结果所必需的,可以用+ 代替。您可以找到有关此功能的更多信息here。
【讨论】: