perl学习笔记(九)用正则表达式处理文本(2)


前言

本文主要记录一些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; #输出到原始文件
}


总结

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值