簡體   English   中英

Perl 多行不匹配

[英]Perl does not match multiple lines

我想匹配:

Start Here Some example
text covering a few
lines. End Here

所以我做

$ perl -nle 'print $1 if /(Start Here.*?)End Here/s'

然后粘貼上面的文本和ctr-D 它與 cmd 不匹配 - 但它在文件腳本中匹配。 為什么?

使用-0命令行開關將輸入記錄分隔符 ( $/ ) 更改為 null。

perl -0777nle 'print $1 if /(Start Here.*?)End Here/s' <<END
Start Here Some example
text covering a few
lines. End Here
THE_END

人perlrun

-0[八進制/十六進制]
將輸入記錄分隔符 ($/) 指定為八進制或十六進制數。 […] 任何 0400 或以上的值都會導致 Perl 對整個文件進行 slurp,但按照慣例,值 0777 是通常用於此目的的值。

人perlvar

IO::Handle->input_record_separator(EXPR)
$INPUT_RECORD_SEPARATOR
$RS
$/
輸入記錄分隔符,默認為換行符。 這影響了 Perl 關於“線”的概念。 [...] 您可以將其設置為 [...] "undef" 以通讀文件末尾。

正如其他人所解釋的那樣,您一次讀取一行文件,因此多行匹配永遠不會起作用。

一次讀取一行文件通常是最好的方法。 所以我們可以使用“flip-flip”操作符來做到這一點:

 $ perl -nle 'print if /Start Here/ .. /End Here/' your_file_here

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM