Home > Mobile >  Why Perl regex doesn't match "\n" with a following character?
Why Perl regex doesn't match "\n" with a following character?

Time:10-10

This is my file foo.txt:

a
b

This is what I'm doing:

$ perl -pi -e 's/\nb/z/g' foo.txt

Nothing changes in the file, while I'm expecting it to become:

az

Why? It's Perl v5.34.0.

CodePudding user response:

The firs time you evaluate the substitution, you match against a␊. The second time, against b␊. So it doesn't match either times.

You want to match against the entire file. You can tell Perl to consider the entire file one line by using -0777.

perl -i -0777pe's/\nb/z/g' foo.txt
  • Related