• 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
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    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;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    9.3 一次性更新多个文件

    #=======================================================================
    #一次更新多个文件
    #=======================================================================
    my $data = localtime; #获取当前电脑的时间
    $^I = "*.bak"; #对原始文件进行备份
    
    while(<>){ #钻石操作符<>实现文件名输入
        s/^Author:.*/Author: LL/; #替换
        s/^Phone:.*\n//; #删除
        s/^Date:.*/Date: $data/; #更新时间
        print; #输出到原始文件
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    总结

  • 相关阅读:
    Qt应用开发(基础篇)——工具按钮类 QToolButton
    力扣:494. 目标和
    生成式 AI 如何释放开发者的生产力?
    Java开发者的网络安全指南(二)
    SpringBoot(一):SpringBoot快速入门
    数据结构-leetcode-环形链表Ⅱ
    Graalvm-21 Windows初体验
    第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(昆明),签到题4题
    Nextcloud缩略图尺寸的文档
    网安学习-Windows权限提升1
  • 原文地址:https://blog.csdn.net/hh199203/article/details/125447373