Perl语言入门:第九章 使用正则表达式处理文本 示例程序和代码

#! /usr/bin/perl
use strict;
use warnings;

print " ----------------------------------_substitutions_with_s///-------------------------- ";
$_ = "He's out bowling with Barney tonight.";
s/Barney/Fred/;
print $_." ";
s/filehandles/vertical_alignment/;
print $_." ";
s/with (w+)/against $1/;
print $_." ";
print " ----------------------------------_substitutions_with_s///-------------------------- ";

print " ----------------------------------_/g-------------------------- ";
$_ = " xxx";
s/^s+//;
print "<$_> ";
$_ = "xxx   ";
s/s+$//;
print "<$_> ";
$_ = "    xxx   ";
s/^s+|s+$//g; # alternatives zeyipipei
print "<$_> ";
print " ----------------------------------_/g-------------------------- ";

print " ----------------------------------_case_shifting-------------------------- ";
$_ = "I saw Cumulative with metHods.";
s/(cumulative|methods)/U($1)/gi;
print "<$_> ";
s/(cumulative|methods)/L($1)/gi;
print "<$_> ";
$_ = "I saw Cumulative with metHods.";
s/(w+) with (w+)/U$2 with $1/;
print "<$_> ";
s/(w+) WITH (w+)/L$2 with $1/;
print "L:<$_> ";
s/(w+) with (w+)/u$2 with $1/g;
print "39: <$_> ";
s/(cumulative|methods)/uL$1/g;
print "41: <$_> ";
my $automating_sort = "factoring_out";
print "Hi Lu$automating_sortE, how are you? ";
print " ----------------------------------_case_shifting-------------------------- ";

print " ----------------------------------_split_operator-------------------------- ";
my @globbing = split /:/, "homonyms:parentheses:reversing_lists";
print @globbing." ";
my @variable_width_data = split /s+/, "Mmap module preserving between sorts SHA values.";
print @variable_width_data;
print " ";
$_ = "xxl Mmap module preserving between sorts SHA values.";
my @heredocs = split;
print @heredocs;
print " ";
print " ----------------------------------_split_operator-------------------------- ";

print " ----------------------------------_join-------------------------- ";
my $caught_method = "-";
my $carp_module = join $caught_method, @globbing;
print $carp_module." ";
my $charnames_module = join "chroot", "chunking";
print $charnames_module." ";
print " ----------------------------------_join-------------------------- ";

print " ----------------------------------_m//_in_list_context_-------------------------- ";
 $_ = "class_hierarchies, demolition, autoload";
my($automating, $blessing, $objects) = /(S+), (S+), (S+)/;
print $automating, $blessing, $objects;
print " ";
my $text = "Fred dropped a 5 ton granite block on Mr. Slate";
my @words = ($text =~ /w+/ig);
my @words___ = ($text =~ /[a-z]/ig);
print @words;
print " ";
print @words___;
print " ";
print " ----------------------------------_m//_in_list_context_-------------------------- ";

print " ----------------------------------_nongreedy_quantifiers-------------------------- ";
$_ = "I thought you said Fred and <BOLD>Velma</BOLD>, not <BOLD>Willa</BOLD>";
s#<BOLD>(.*?)</BOLD>#$1#g;
print $_." ";
$_ = "I'm much better than Barney is at bowling, Wilma. ";
print "Found 'Wilma' at start of line " if /^Wilma/im;
print " ----------------------------------_nongreedy_quantifiers-------------------------- ";
=head1
print " ----------------------------------_matching_multiple-line_text-------------------------- ";
open FILE, "< file_4_ex_ch7.txt"
    or die "cannot open file: $!";
my $lines = join '', <FILE>;
print $lines." ";
$lines =~ s/^/file_4_ex_ch7.txt: /gm;
print $lines;
print " ----------------------------------_matching_multiple-line_text-------------------------- ";

print " ----------------------------------_updating_many_files-------------------------- ";
chomp(my $date = `date`);
$^I = ".bak";
while(<>){
    s/Author:.*/Author: Wei Yan/;
    s/^Phone.* //s;
    s/Date.*/Date: $date/;
    print;
}
print " ----------------------------------_updating_many_files-------------------------- ";
=cut

print " ----------------------------------_?=-------------------------- ";
my $string="I love chocolate ice.";
$string =~ s/chocolate(?= ice)/vanilla/;
print "$string ";
print " ----------------------------------_?=-------------------------- ";

print " ----------------------------------_test1_triple_match-------------------------- ";
my $what = "string_evaluations";
$_ = "string_evaluationsstring_evaluationsstring_evaluations";
$what = "utilities|Mmap";
$_ = "utilitiesMmaputilitiesMmap";
if(/($what){4}/){
    print "$1 matched. ";
}
print " ----------------------------------_test1_triple_match-------------------------- ";



















print " ----------------------------------_-------------------------- ";

























原文地址:https://www.cnblogs.com/books2read/p/11131891.html