包括加号登录preg_match_all



我想解析这种格式的字符串++VAR++。

我尝试过preg_match_all("++.*?++",$input,$matches);但失败了。我错过了什么?

你错过了转义+,也忘记添加php分隔符。

preg_match_all('~++.*?++~', $input, $matches);

您的正则表达式缺少正则表达式分隔符。此外,+是一个特殊的正则表达式元字符(一个或多个量词),则必须对其进行转义才能将其视为文字字符。

如果只有字母数字字符在双加号内匹配,则可以使用

preg_match_all('~(?<!+)++(w+)++(?!+)~', $txt, $matches);

请参阅此正则表达式演示。

参见 IDEONE 演示:

$re = '~(?<!+)++(w+)++(?!+)~'; 
$str = "I have ++VAR++ and  +++++++++++ and +++text+++++ and ++ANOTHER_VAR++."; 
preg_match_all($re, $str, $matches);
print_r($matches);

最新更新