Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
625 views
in Technique[技术] by (71.8m points)

perl regex not matching string with newline character

I'm trying to use perl (v5.14.2) via a bash shell (GNU Bash-4.2) in Kubuntu (GNU/Linux) to search and replace a string that includes a newline character, but I'm not succeeding yet.

Here's the text file I'm searching:

<!-- filename: prac1.html -->

hello
kitty

blah blah blah

When I use a text editor's (Kate's) search-and-replace functionality or when I use a regex tester (http://regexpal.com/), I can easily get this regex to work:

hello
kitty

But when using perl in the command line, none of the following commands have worked:

perl -p -i -e 's,hello
kitty,newtext,' prac1.html
perl -p -i -e 's,hello.kitty,newtext,s' prac1.html
perl -p -i -e 's,hello.*kitty,newtext,s' prac1.html
perl -p -i -e 's,hello[Ss]kitty,newtext,' prac1.html
perl -p -i -e 's,hello[Ss]*kitty,newtext,' prac1.html

Actually, I got desperate and tried many other patterns, including all of these (different permutations in the "single-line" and "multi-line" modes):

perl -p -i -e 's,hello
kitty,newtext,' prac1.html
perl -p -i -e 's,hello.kitty,newtext,' prac1.html
perl -p -i -e 's,hello
kitty,newtext,s' prac1.html
perl -p -i -e 's,hello.kitty,newtext,s' prac1.html
perl -p -i -e 's,hello
kitty,newtext,m' prac1.html
perl -p -i -e 's,hello.kitty,newtext,m' prac1.html
perl -p -i -e 's,hello
kitty,newtext,ms' prac1.html
perl -p -i -e 's,hello.kitty,newtext,ms' prac1.html

perl -p -i -e 's,hello[Ss]kitty,newtext,' prac1.html
perl -p -i -e 's,hello[Ss]*kitty,newtext,' prac1.html
perl -p -i -e 's,hello$[Ss]^kitty,newtext,' prac1.html
perl -p -i -e 's,hello$[Ss]*^kitty,newtext,' prac1.html
perl -p -i -e 's,hello[Ss]kitty,newtext,s' prac1.html
perl -p -i -e 's,hello[Ss]*kitty,newtext,s' prac1.html
perl -p -i -e 's,hello$[Ss]^kitty,newtext,s' prac1.html
perl -p -i -e 's,hello$[Ss]*^kitty,newtext,s' prac1.html
perl -p -i -e 's,hello[Ss]kitty,newtext,m' prac1.html
perl -p -i -e 's,hello[Ss]*kitty,newtext,m' prac1.html
perl -p -i -e 's,hello$[Ss]^kitty,newtext,m' prac1.html
perl -p -i -e 's,hello$[Ss]*^kitty,newtext,m' prac1.html
perl -p -i -e 's,hello[Ss]kitty,newtext,ms' prac1.html
perl -p -i -e 's,hello[Ss]*kitty,newtext,ms' prac1.html
perl -p -i -e 's,hello$[Ss]^kitty,newtext,ms' prac1.html
perl -p -i -e 's,hello$[Ss]*^kitty,newtext,ms' prac1.html

(I also tried using R f D etc., and global mode as well.)

Can anyone spot the issue or suggest a solution?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try doing this, I make this possible by modifying the input record separator (a newline by default) :

perl -i -p00e 's,hello
kitty,newtext,' prac1.html

from perldoc perlrun :

-0[octal/hexadecimal]

specifies the input record separator ($/ ) as an octal or hexadecimal number. If there are no digits, the null character is the separator. Other switches may precede or follow the digits. For example, if you have a version of find which can print filenames terminated by the null character, you can say this:

find . -name '*.orig' -print0 | perl -n0e unlink

The special value 00 will cause Perl to slurp files in paragraph mode. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...