Perl で複数行の正規表現を行う方法については、多くの質問があります。それらのほとんどはs、ドットを改行に一致させるスイッチについて言及しています。ただし、正確なフレーズ(パターンではない)に一致させたいのですが、改行がどこにあるのかわかりません。問題は、改行を と一致させるのではなく、無視.できるかということです。
MWE:
$pattern = "Match this exact phrase across newlines";
$text1 = "Match\nthis exact\nphrase across newlines";
$text2 = "Match this\nexact phra\nse across\nnewlines";
$text3 = "Keep any newlines\nMatch this exact\nphrase across newlines\noutside\nof the match";
$text1 =~ s/$pattern/replacement text/s;
$text2 =~ s/$pattern/replacement text/s;
$text3 =~ s/$pattern/replacement text/s;
print "$text1\n---\n$text2\n---\n$text3\n";
スペース ( ) の代わりにドットをパターンに入れることができます"Match.this.exact.phrase"が、2 番目の例ではうまくいきません。前処理としてすべての改行を削除できますが、一致の一部ではない改行を保持したいと思います (3 番目の例のように)。
望ましい出力:
replacement text
---
replacement text
---
Keep any newlines
replacement text
outside
of the match