如何匹配perl正则表达式中的换行符?

Sop*_*unk 2 regex shell perl

我正在尝试研究如何使用perl(来自shell)匹配换行符.以下:

(echo a b c d e; echo f g h i j; echo l m n o p) | perl -pe 's/(c.*)/[$1]/'
Run Code Online (Sandbox Code Playgroud)

我明白了:

a b [c d e]
f g h i j
l m n o p
Run Code Online (Sandbox Code Playgroud)

这是我的期望.但是当我/s在我的正则表达式的末尾放置一个时,我得到了这个:

a b [c d e
]f g h i j
l m n o p
Run Code Online (Sandbox Code Playgroud)

我期望并希望它打印的是:

a b [c d e
f g h i j
l m n o p
]
Run Code Online (Sandbox Code Playgroud)

我的正则表达式有问题,或者我的perl调用标志?

小智 10

-p循环输入逐行输入,其中"lines"由$/输入记录分隔符分隔,默认情况下是换行符.如果你想将所有STDIN啜饮$_进行匹配,请使用-0777.

$ echo "a b c d e\nf g h i j\nl m n o p" | perl -pe 's/(c.*)/[$1]/s'
a b [c d e
]f g h i j
l m n o p
$ echo "a b c d e\nf g h i j\nl m n o p" | perl -0777pe 's/(c.*)/[$1]/s'
a b [c d e
f g h i j
l m n o p
]
Run Code Online (Sandbox Code Playgroud)

有关这两个标志的信息,请参阅perlrun中的命令开关.-l(破折号)也很有用.