構文 |
|
qr/文字列/マッチオプション |
返り値 |
|
正規表現へのリファレンス |
説明
- 正規表現を変数に格納できるようになります。
- 可能であれば正規表現はコンパイルされるので,繰り返し利用される場合は高速に処理されます。
使用例
#!/usr/bin/perl
use strict;
use warnings;
my $regexp = qr/\.pdf$/;
my @files = qw(
data.pdf
img1.png
img2.PNG
sample.PDF
index.html
DEFAULT.HTM
);
foreach my $file (@files) {
if ($file =~ $regexp) {
print $file, "\n";
}
}
#!/usr/bin/perl
use strict;
use warnings;
my $regexp = qr/\.pdf$/i; # オプションを指定できる
my @files = qw(
data.pdf
img1.png
img2.PNG
sample.PDF
index.html
DEFAULT.HTM
);
foreach my $file (@files) {
if ($file =~ $regexp) {
print $file, "\n";
}
}
#!/usr/bin/perl
use strict;
use warnings;
my $regexp = qr/\.txt$/i;
my @files = qw(
data.pdf
README.TXT
img2.png
sample.txt
index.html
DEFAULT.HTM
);
foreach (@files) {
my $file = $_;
$file =~ s/$regexp/.bak/;
print $file, "\n";
}