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

 

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

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

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

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

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

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

print "\n----------------------------------_m//_in_list_context_--------------------------\n";
 $_ = "class_hierarchies, demolition, autoload";
my($automating, $blessing, $objects) = /(\S+), (\S+), (\S+)/;
print $automating, $blessing, $objects;
print "\n";
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 "\n";
print @words___;
print "\n";
print "\n----------------------------------_m//_in_list_context_--------------------------\n";

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

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

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

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



















print "\n----------------------------------_--------------------------\n";

























转载于:https://www.cnblogs.com/books2read/p/11131891.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值