preg 正则表达式:需要同时捕获关键字和后面用大括号括起来的 ID
preg Regex: Need to capture both a keyword and an ID that follows it enclosed in braces
假设有一串文本
blah blah blah CONTENT{asdfkuydiusyhv} blah blah blah VALUE{jdiuvhgy54bkd8} blah blah blah
我需要一个 php 正则表达式 (preg_match_all),它将所有关键字和值捕获到一个数组中,其中
$array = 0=> $keywords[0] = CONTENT
$keywords[1] = VALUE
1=> $results[0] = asdfkuydiusyhv
$results[1] = jdiuvhgy54bkd8
如何操作?
如果关键字总是大写,如果不调整用i
或添加a-z
:
preg_match_all('/ ([A-Z]+){([^}]+)}/', $string, $matches);
$keywords = $matches[1];
$results = $matches[2];
要查找特定关键字,例如:
$keywords = array('CONTENT'); //etc..
foreach($keywords as $keyword) {
preg_match("/ $keyword{([^}]+)}/", $string, $match);
$results[] = $match[1];
}
假设有一串文本
blah blah blah CONTENT{asdfkuydiusyhv} blah blah blah VALUE{jdiuvhgy54bkd8} blah blah blah
我需要一个 php 正则表达式 (preg_match_all),它将所有关键字和值捕获到一个数组中,其中
$array = 0=> $keywords[0] = CONTENT
$keywords[1] = VALUE
1=> $results[0] = asdfkuydiusyhv
$results[1] = jdiuvhgy54bkd8
如何操作?
如果关键字总是大写,如果不调整用i
或添加a-z
:
preg_match_all('/ ([A-Z]+){([^}]+)}/', $string, $matches);
$keywords = $matches[1];
$results = $matches[2];
要查找特定关键字,例如:
$keywords = array('CONTENT'); //etc..
foreach($keywords as $keyword) {
preg_match("/ $keyword{([^}]+)}/", $string, $match);
$results[] = $match[1];
}