Php 正则表达式来获取两个字符串之间的任何内容'$';和'';前面没有'//';

Php 正则表达式来获取两个字符串之间的任何内容'$';和'';前面没有'//';,php,regex,preg-match,preg-match-all,regex-lookarounds,Php,Regex,Preg Match,Preg Match All,Regex Lookarounds,我得到了以下代码 $original_file=str_replace('<?php','',file_get_contents('file.txt')); //regular expression to get anything that starts with '$' and end with ';' preg_match_all("/\\$(.*?);/",$original_file,$matches,PREG_PATTERN_ORDER); $original\u file=

我得到了以下代码

$original_file=str_replace('<?php','',file_get_contents('file.txt'));

//regular expression to get anything that starts with '$' and end with ';'
preg_match_all("/\\$(.*?);/",$original_file,$matches,PREG_PATTERN_ORDER);
$original\u file=str\u replace(”您可以尝试:

preg_match_all("~(?<!//)\$([^;]*);~", $original_file, $matches, PREG_PATTERN_ORDER);

preg\u match\u all(“~(?As@anubhava,但使用
s
选项遍历所有
$str

preg_match_all('/(?<!\\\)\$([^;]+);/s', $str, $m, PREG_PATTERN_ORDER);

对于完整的“模板解析”"查看概念和讨论。

\\不是PHP注释,而是//如果您试图在PHP中解析PHP,您可以查看。或者甚至函数。Guillaume Poussel,不,我不是,但信息非常有趣,我可能会在将来使用它。anubhava,我的错误前面的字符串是“//”而不是“\\”,它是否应该像pre一样g_match_all(“/”(?这对你合适吗?preg_match_all(“/”)是的,看起来也很好。Hello@user1518071,这不仅仅是“精确性”,你自己检查测试(我现在编辑了一个
$str
值),显示“通用匹配”的错误情况。
preg_match_all(
    '/(?<!\\\)\$([a-z0-9_]+);/si', 
    $str, 
    $m, 
    PREG_PATTERN_ORDER
);
$str = "\nTEST\n\n \$x;\$y;\ny\$y_ \$_X;";