C:\>perl -0 -pe "s/<[^>]*>//g;"
C:\>perl -e "s|<(.*?)>(.*?)</$1>|$2|m"
D:\>perl -ne "s|<(.*)>(.*)<\/\1>|$2|ig; print;" test.html test D:\>perl -ne "print;" test.html <html>test</html> D:\>
D:\>perl -ne "while(s|<(.*)>(.*)<\/\1>|$2|g) { print \"$_\n\"; }" test.html <head><title>PerlTest</title></head><body>PerlTestBody</body> <title>PerlTest</title>PerlTestBody PerlTestPerlTestBody D:\>perl -ne "print" test.html <html><head><title>PerlTest</title></head><body>PerlTestBody</body></html> D:\>
D:\>perl -ne "while(s|<(.*)>(.*)<\/\1>|$2|g) { }print \"$_\n\";" test.html PerlTestPerlTestBody D:\>perl -ne "print" test.html <html><head><title>PerlTest</title></head><body>PerlTestBody</body></html> D:\>
D:\>perl -ne "print;" test.html <html><head><title>PerlTest</title></head> <body>PerlTestBody</body> </html> D:\>perl -ne "while(s|<(.*)>(.*)<\/\1>|$2|g) { }print \"$_\n\";" test.html <html>PerlTest PerlTestBody </html> D:\>
D:\>perl -ne "$f.=$_; END{ while($f=~s|<(.*)>(.*)<\/\1>|$2|sg){} print \"$f\";}" test.html PerlTest PerlTestBody D:\>perl -ne "print" test.html <html><head><title>PerlTest</title></head> <body>PerlTestBody</body> </html> D:\>
D:\>perl -ne "$f.=$_; END{ while($f=~s|<(.*)>(.*)<\/\1>|$2|sg){} print \"$f\";}" test.html PerlTest PerlTestBody <hr>AllAboutPerlOneLiner D:\>perl -pe "" test.html <html><head><title>PerlTest</title></head> <body> <h1>PerlTestBody</h1> <hr><p>AllAboutPerlOneLiner</p> </body> </html> D:\>
D:\>perl -ne "$f.=$_; END{ while($f=~s|<(.*) .*>(.*)<\/\1>|$2|sg){} print \"$f\";}" test.html <html><head><title>PerlTest</title></head> <body> <h1>PerlTestBody</h1> <hr><p>AllAboutPerlOneLiner</p> Google </body> </html> D:\>perl -pe "" test.html <html><head><title>PerlTest</title></head> <body> <h1>PerlTestBody</h1> <hr><p>AllAboutPerlOneLiner</p> <a href="http://www.google.com">Google</a> </body> </html> D:\>
D:\>perl -ne "$f.=$_; END{ while($f=~s|<(.*) .*>(.*)<\/\1>|$2|sg){} while($f=~s|<(.*)>(.*)<\/\1>|$2|sg){} print \"$f\";}" test.html PerlTest PerlTestBody <hr>AllAboutPerlOneLiner Google D:\>perl -pe "" test.html <html><head><title>PerlTest</title></head> <body> <h1>PerlTestBody</h1> <hr><p>AllAboutPerlOneLiner</p> <a href="http://www.google.com">Google</a> </body> </html> D:\>
D:\>perl -0777 -e "$_=<>; while(s|<(.*) .*>(.*)<\/\1>|$2|sg){} while(s|<(.*)>(.*)<\/\1>|$2|sg){} print;" test.html PerlTest PerlTestBody <hr>AllAboutPerlOneLiner Google D:\>perl -pe "" test.html <html><head><title>PerlTest</title></head> <body> <h1>PerlTestBody</h1> <hr><p>AllAboutPerlOneLiner</p> <a href="http://www.google.com">Google</a> </body> </html> D:\>
D:\>perl -0777 -e "$_=<>; while(s|<(.*?) .*?>(.*?)<\/\1>|$2|sg){} while(s|<(.*?)>(.*?)<\/\1>|$2|sg){} print;" test.html PerlTest PerlTestBody <hr>AllAboutPerlOneLiner,SearchOnGoogleWithSomeWords.Google'sLogo<img href="http://www.google.com" alt="GoogleLogo">IsHere. Google D:\>perl -pe "" test.html <html><head><title>PerlTest</title></head> <body> <h1>PerlTestBody</h1> <hr><p>AllAboutPerlOneLiner,SearchOn<a href="http://www.google.com">Google</a>WithSomeWords.Google'sLogo<img href="http://www.google.com" alt="GoogleLogo">IsHere.</p> <a href="http://www.google.com">Google</a> </body> </html> D:\>
D:\>perl -0 -pe "s/<[^>]*>//g;" test.html PerlTest PerlTestBody AllAboutPerlOneLiner,SearchOnGoogleWithSomeWords.Google'sLogoIsHere. Google D:\>perl -pe "" test.html <html><head><title>PerlTest</title></head> <body> <h1>PerlTestBody</h1> <hr><p>AllAboutPerlOneLiner,SearchOn<a href="http://www.google.com">Google</a>WithSomeWords.Google'sLogo<img href="http://www.google.com" alt="GoogleLogo">IsHere.</p> <a href="http://www.google.com">Google</a> </body> </html> D:\>
D:\>perl -0 -pe "s/<[^>]*>//g;" test.html PerlTest PerlTestBody AllAboutPerlOneLiner,SearchOnGoogleWithSomeWords. Google'sLogoIsHere. Google D:\>perl -pe "" test.html <html><head><title>PerlTest</title></head> <body> <h1>PerlTestBody</h1> <hr><p>AllAboutPerlOneLiner,SearchOn<a href="http://www.google.com">Google</a>WithSomeWords. Google'sLogo<img href="http://www.google.com" alt="GoogleLogo">IsHere.</p> <a href="http://www.google.com">Google</a> </body> </html> D:\>
簡易と銘打っているならやっぱりスクリプトが長いのはよろしくないなぁということで。最初にあげたものに帰着したんだな。わざわざ-0をつけているのは1ファイルの内容をNULL値まで一気に$_に代入ということなんだけど、短い割りには上手いことHTMLタグを処理しているんだな。コンパイラはこれを下のように解釈しているんだな。
while (<>) { s/<[^>]*>//g; } continue { print; }
つまり、<で始まって、>以外の文字列が続いて、>で終わっているところは全て削除という操作をしているんだな。だから下手すると、意図しないところでマッチングされてしまうこともあるんだな。間違って<や>を文中で使用してしまうとそこも削除されてしまう可能性があるということ。