前言
本文主要记录一些perl中,如何用正则表达式处理字符串的基本方法,包括:
- 非贪婪量词
- 跨行的模式匹配
- 一次性更新多个文件
9 用正则表达式处理文本(2)
9.1 非贪婪量词
#=======================================================================
#量词后边加上一个问号,就将贪婪量词转变为非贪婪量词
#贪婪模式默认是匹配最后一个,非贪婪模式匹配的是第一个
# *? +? ?? {5,10}?
#=======================================================================
$_ = "I thought you said Fred and <BOLD>Velma</BOLD>, not <BOLD>Wilma</BOLD>.\n";
#贪婪模式,</BOLD>默认的是匹配最后一个,所以(.*)匹配到的是Velma</BOLD>, not <BOLD>Wilma
s#<BOLD>(.*)</BOLD>#$1#g;
print; #I thought you said Fred and Velma</BOLD>, not <BOLD>Wilma.
$_ = "I thought you said Fred and <BOLD>Velma</BOLD>, not <BOLD>Wilma</BOLD>.\n";
#非贪婪模式,</BOLD>匹配第一个
s#<BOLD>(.*?)</BOLD>#$1#g;
print; #I thought you said Fred and Velma, not Wilma.
$_ = "helloooooooooooo";
#贪婪模式
if(/(hello+)/){
print "$1\n"; #helloooooooooooo
}
#非贪婪模式
if(/(hello+?)/){
print "$1\n"; #hello
}
9.2 跨行的模式匹配
#=======================================================================
#跨行的模式匹配
#m选项跨行 g选项替换全部
#=======================================================================
$_ = "this is the 1 line\nthis is the 2 line\nthis is the 3 line\n";
s/^this/that/; #只匹配了第一行
print;
#that is the 1 line
#this is the 2 line
#this is the 3 line
$_ = "this is the 1 line\nthis is the 2 line\nthis is the 3 line\n";
s/^this/that/mg; #m匹配每一行,g表示匹配多个
print;
#that is the 1 line
#that is the 2 line
#that is the 3 line
#在文件的每一行前面加入文件名
$filename = "test.c";
open FILE, $filename or die "Can't open '$filename': $!";
my $lines = join '', <FILE>; #将文件中每一行用join函数连起来
$lines =~ s/^/$filename: /gm;
9.3 一次性更新多个文件
#=======================================================================
#一次更新多个文件
#=======================================================================
my $data = localtime; #获取当前电脑的时间
$^I = "*.bak"; #对原始文件进行备份
while(<>){ #钻石操作符<>实现文件名输入
s/^Author:.*/Author: LL/; #替换
s/^Phone:.*\n//; #删除
s/^Date:.*/Date: $data/; #更新时间
print; #输出到原始文件
}