我输入了类似以下示例的输入,并且只需要替换所有\input{.*}
命令,这些命令前面没有%
一行。
输入是 LaTeX 代码,其中%
开始注释。这意味着%
当前行之后的所有内容都不应被解释为实际代码,而实际上只是一个注释。(即使它看起来像代码)
示例输入:
this is \input{REAL.tex} real content % \input{COMMENT.tex}
foo \input{REAL.tex} bar
\input{REAL.tex}
%\input{COMMENT.tex}
\input{REAL.tex} % comment
我当前的代码:
$r = "/^(?P<prefix>(?!.*%).*)\\\\input[{\s]+(?P<filename>.*?)[\s}](?P<suffix>.*)$/m";
$data = preg_replace($r, "REPLACED", $data);
echo $data . PHP_EOL;
当前示例输出:
this is \input{REAL.tex} real content % \input{COMMENT.tex}
foo REPLACED bar
REPLACED
%\input{COMMENT.tex}
\input{REAL.tex} % comment
预期的示例输出:
this is REPLACED real content % \input{COMMENT.tex}
foo REPLACED bar
REPLACED
%\input{COMMENT.tex}
REPLACED % comment
问题:不幸的是,由于前瞻断言,由于中间\inputs
的注释,我的正则表达式完全忽略了第一行和最后一行中的。%
(?!.*%)
Question: Do you see a way to achieve the desired output via regular expressions? The \input{REAL.tex}
on the first and last line should be replaced as well.