提问人:atapaka 提问时间:10/26/2023 最后编辑:Barmaratapaka 更新时间:10/26/2023 访问量:42
使用跨多行的递归进行 Perl 匹配文本
Perl matching text using recursions spanning multiple lines
问:
在下面的文字中,我正在尝试匹配.regex101.com 示例适用于正则表达式:\rem{lp}{some text with nested curly braces}
([^\s]*\s*)\\rem\{(?:lp|db)\}(\{((?>[^{}]|\n|(?2))*)\})(\s?[^\s]*)
我希望它也能在perl中工作:
/([^\s]*\s*)\\rem\{(?:lp|db)\}(\{((?>[^{}]|\n|(?2))*)\})(\s?[^\s]*)/g
但是,perl 版本失败(该文件包含以下文本)。这似乎是由于换行符造成的。我该如何纠正这个问题(我也尝试在没有帮助的情况下添加修饰符?text.txt
m
perl -ne 'while(/([^\s]*\s*)\\rem\{(?:lp|db)\}(\{((?>[^{}]|\n|(?2))*)\})(\s?[^\s]*)/g){print ++$a."$&\n";}' test.txt
test.txt
Some text etc word\rem{lp}{ more text {\ce{O2}} further text {\ce{H2O}} . some other text the $m = 48$ ({\ce{O2}}) ps}.
\rem{lp}{ more text {\ce{O2}} further text {\ce{H2O}} . some other text the $m = 48$ ({\ce{O2}}) ps
where more text {\ce{O2}} further text {\ce{H2O}} . some other text the $m = 48$ ({\ce{O2}}) ps from
who more text {\ce{O2}} further text {\ce{H2O}} . some other text the $m = 48$ ({\ce{O2}}) ps.}
\rem{lp}{ more text {\ce{O2}} further text {\ce{H2O}} . some other text the $m = 48$ ({\ce{O2}}) ps.
more text {\ce{O2}} further text {\ce{H2O}} . some other text the $m = 48$ ({\ce{O2}}) ps.}
\subsection{other}
Our bla bla bla
答:
1赞
atapaka
10/26/2023
#1
以防万一有人偶然发现这一点。Perl 不会在一行中啜饮,因此该部分是逐行读取的。为了解决这个问题,人们必须运行while
perl -0777 -ne 'while(/([^\s]*\s*)\\rem\{(?:lp|db)\}(\{((?>[^{}]|\n|(?2))*)\})(\s?[^\s]*)/g){print ++$a."$&\n";}' test.txt
评论
LF
CRLF
dos2unix
\r?\n
\n
\n
[^{}]
{some text with nested curly braces}
\rem{lp}{some text with nested curly braces}
{\ce{O2}}