如何应用此正则表达式?在Php 我有代码
$a = "{temp}name_temp1{/temp} Any thing Any thing {temp}name_temp2{/temp}";
我只需要 name_temp1 和 name_temp2
{temp} 中的任何名称 {/ temp}
谢谢你
答案 0 :(得分:3)
您可以使用延迟量词:
{temp} # look for {temp}
(?P<value>.+?) # anything else afterwards
{/temp} # look for {/temp}
<小时/> 在
PHP
中,这将是:
<?php
$a = "{temp}name_temp1{/temp} Any thing Any thing {temp}name_temp2{/temp}";
$regex = '~{temp}(?P<value>.+?){/temp}~';
preg_match_all($regex, $a, $matches, PREG_SET_ORDER);
foreach($matches as $match) {
echo $match["value"];
}
?>
答案 1 :(得分:2)
试试这个正则表达式:{temp}(.*?){\/temp}
你可以在PHP中使用它:
$a = "{temp}name_temp1{/temp} Any thing Any thing {temp}name_temp2{/temp}";
preg_match_all('/{temp}(.*?){\/temp}/', $a, $matches);
var_dump($matches[1]); // Returns ['name_temp1', 'name_temp2']