[英]Perl does not match multiple lines
I want to match:我想匹配:
Start Here Some example
text covering a few
lines. End Here
So I do所以我做
$ perl -nle 'print $1 if /(Start Here.*?)End Here/s'
then paste the text above and ctr-D
.然后粘贴上面的文本和ctr-D
。 It wont match from cmd - but it does in file script.它与 cmd 不匹配 - 但它在文件脚本中匹配。 Why?为什么?
Change input record separator ( $/
) to null using -0
command line switch.使用-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
-0[octal/hexadecimal] -0[八进制/十六进制]
specifies the input record separator ($/) as an octal or hexadecimal number.将输入记录分隔符 ($/) 指定为八进制或十六进制数。 […] Any value 0400 or above will cause Perl to slurp files whole, but by convention the value 0777 is the one normally used for this purpose. […] 任何 0400 或以上的值都会导致 Perl 对整个文件进行 slurp,但按照惯例,值 0777 是通常用于此目的的值。
IO::Handle->input_record_separator( EXPR ) IO::Handle->input_record_separator(EXPR)
$INPUT_RECORD_SEPARATOR $INPUT_RECORD_SEPARATOR
$RS $RS
$/ $/
The input record separator, newline by default.输入记录分隔符,默认为换行符。 This influences Perl's idea of what a "line" is.这影响了 Perl 关于“线”的概念。 […] You may set it to […] "undef" to read through the end of file. [...] 您可以将其设置为 [...] "undef" 以通读文件末尾。
As others have explained, you're reading your file a line at a time, so matches over multiple lines are never going to work.正如其他人所解释的那样,您一次读取一行文件,因此多行匹配永远不会起作用。
Reading files a line at a time is often the best approach.一次读取一行文件通常是最好的方法。 So we can use the "flip-flip" operator to do this:所以我们可以使用“flip-flip”操作符来做到这一点:
$ perl -nle 'print if /Start Here/ .. /End Here/' your_file_here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.