正则表达式查找%PHP中的所有子字符串

正则表达式查找%PHP中的所有子字符串,php,regex,Php,Regex,我想找到字符串中“%”内的所有子字符串,但我不明白为什么它只找到“id” $test=''; $token_regex_inside_tags=“/]*%([\w]+)%[^>]*])>/; preg_match_all($token_regex_in_tags,$test,$matches); 假设:-我假设您只需要在%之间找到中的内容 您可以使用这个正则表达式,它使用 (?:\G(?!\A)|<)[^%>]*%([^%>]*)% (?:\G(?!\A)|]*%([^%>]

我想找到字符串中“%”内的所有子字符串,但我不明白为什么它只找到“id”

$test='';
$token_regex_inside_tags=“/]*%([\w]+)%[^>]*])>/;
preg_match_all($token_regex_in_tags,$test,$matches);

假设:-我假设您只需要在
%
之间找到
中的内容

您可以使用这个正则表达式,它使用

(?:\G(?!\A)|<)[^%>]*%([^%>]*)%
(?:\G(?!\A)|]*%([^%>]*)%

(?:\G(?!\A)|<)[^%>]*%([^%>]*)%
正则表达式分解

(?)
\G(?!\A)#上一场比赛结束
|#交替
<#匹配<字面意思
)
[^%>]*#找到任何不是%or>
%([^%>]*)%#查找%
在你的正则表达式中

< #Matches < literally
 (
   [^>]* #Moves till > is found. Here its in end
   %([\w]+)% #This part backtracks from last but is just able to find only the last content within two %
   [^>]*
)>
<#匹配<字面意思
(
[^>]*#移动直到找到>为止。到此结束
%([\w]+)%#此部分从上一部分开始回溯,但仅能在2%内找到最后一部分内容
[^>]*
)>

感谢您的详细解释!
< #Matches < literally
 (
   [^>]* #Moves till > is found. Here its in end
   %([\w]+)% #This part backtracks from last but is just able to find only the last content within two %
   [^>]*
)>