我有一个perl脚本,其示例如下:
#/usr/bin/perl -w
print 'My output: ';
print <<END;
Here is more content
which is printed with
heredoc style
END
print 'End of output';
现在我希望用JavaScript提取上述heredoc打印的内容。结果应如下所示:
<<END;
Here is more content
which is printed with
heredoc style
END
我用<<END(.|\n)*END
尝试过。如果文档只包含一个heredoc,但是如果它包含多个heredoc则不行。
例如,如果我的perl脚本如下所示:
#/usr/bin/perl -w
print 'My output: ';
print <<END;
Here is more content
which is printed with
heredoc style
END
print <<END;
Here is even more content
which is printed with
heredoc style
END
print 'End of output';
正则表达式匹配:
<<END;
Here is more content
which is printed with
heredoc style
END
print <<END;
Here is even more content
which is printed with
heredoc style
END
但它应与
匹配<<END;
Here is more content
which is printed with
heredoc style
END
和
<<END;
Here is even more content
which is printed with
heredoc style
END
有没有人有想法,我的正则表达式出了什么问题?
另一个问题:是否可以仅使用正则表达式来捕获未指定为heredoc字符串END
的所有heredoc?
答案 0 :(得分:2)
问题是*
默认为“贪婪”。 *
捕获它可以匹配的所有内容,直到*
之前的模式失败。只有这样才会回归。在您的情况下,模式一直有效,直到字符串结束。
为了防止它贪婪并检查它是否已经过了它应该结束的点(看我在那里做了什么?:D),在?
之后添加*
。
<<END(.|\n)*?END