Perl是一種功能強大的編程語言,它內置了對正則表達式的支持
my $file = "example.txt";
open(my $fh, '<', $file) or die "Could not open file '$file' $!";
my $content = do { local $/; <$fh> };
close($fh);
my @emails = $content =~ /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/g;
$content =~ s/apple/orange/g;
my @values = split(/,/, $content);
my ($host, $path) = $content =~ /(?:([a-zA-Z0-9.-]+)(?::[0-9]+)?)(\/.*)/;
my @emails = $content =~ /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/gi;
通過結合這些技巧,你可以在Perl中有效地使用正則表達式來查找、替換、分割和處理文件內容。