あなたの天然記念物
ホーム更新雑談Perl鉄ゲタランドナーコースガイド自転車Linuxリンク経歴連絡先

正規表現や置換の区切り文字を選べる (2017.07.17)

こんなツイートを見た

サンプルコード

LinuxとWindowsで動作確認済。
#!/usr/local/bin/perl -w

use utf8;
use warnings;
use strict;
use open IO => ":utf8";

use Encode::Locale;

binmode STDOUT, ":encoding(console_out)";

$| = 1;

my $text = "efghogefgh\n";

print $text =~ s!hoge!fuga!r;
print $text =~ s@hoge@fuga@r;
print $text =~ s#hoge#fuga#r;
print $text =~ s$hoge$fuga$r;
print $text =~ s%hoge%fuga%r;
print $text =~ s^hoge^fuga^r;
print $text =~ s&hoge&fuga&r;
print $text =~ s*hoge*fuga*r;
print $text =~ s-hoge-fuga-r;
print $text =~ s=hoge=fuga=r;
print $text =~ s+hoge+fuga+r;
print $text =~ s|hoge|fuga|r;
print $text =~ s\hoge\fuga\r;
print $text =~ s~hoge~fuga~r;
print $text =~ s`hoge`fuga`r;
print $text =~ s:hoge:fuga:r;
print $text =~ s;hoge;fuga;r;
print $text =~ s"hoge"fuga"r;
print $text =~ s'hoge'fuga'r;
print $text =~ s,hoge,fuga,r;
print $text =~ s.hoge.fuga.r;
print $text =~ s?hoge?fuga?r;
print $text =~ s/hoge/fuga/r;

実行結果

efgfugafgh
efgfugafgh
efgfugafgh
efgfugafgh
efgfugafgh
efgfugafgh
efgfugafgh
efgfugafgh
efgfugafgh
efgfugafgh
efgfugafgh
efgfugafgh
efgfugafgh
efgfugafgh
efgfugafgh
efgfugafgh
efgfugafgh
efgfugafgh
efgfugafgh
efgfugafgh
efgfugafgh
efgfugafgh
efgfugafgh