Python正则表达式学习心得

Python正则表达式

  • 正则表达式主要用于字符串处理,可以快速、准确地完成复杂的查找、替换等。
  • Python标准库re提供了正则表达式操作所需要的功能。
1.1 正则表达式语法

使用元字符来配合匹配字符串。常用元字符列表如下(简略):

  • 元字符描述
    .匹配除换行符以外的任意单个字符,单行模式下可以匹配换行符
    *匹配0或多次前边的字符
    +匹配1或多次前边的字符
    |或,前后字符,二选一
    ^匹配开头,以^后边的字符开头的字符串
    $匹配行尾,以$前边的字符结尾的字符串
    匹配0或1次前边出现的字符
    \转义字符,/后边的特殊字符转义为普通字符
    \d匹配任何数字,1个,相当于[0-9]
    \w匹配任何字母、数字、下划线,1个,相当于[a-zA-Z0-9]
    {m,n}至少匹配m次,最多匹配n次
    []匹配位于[]中的任意一个字符
1.2 re模块的主要函数
  • 函数描述
    compile(表达式[,标志])创建正则表达式对象
    search(表达式,字符串[,标志])在整个字符串中寻找,返回Match对象或None
    match(表达式,字符串[,标志])从字符串的开始处匹配,返回Match对象或None
    findall(表达式,字符串[,标志])返回所有匹配的字符串列表
1.3 简单用法案例
  • import re   # 引入re库
    
    text=''''RegExr was created  by gskinner.com and is proudly123 hosted by Media Temple. phone:15278334412
    Edit the Expression & Text to see345 matches. Roll over matches or the expression for details. PCRE & JavaScript flavors of RegEx are supported. Validate your expressionwith Tests mode.'python、jython'http://www.python.org'、'http://python.org'、www.pypython.org、'www.python.org'和'python.org"'''
    
    #正则表达式的简单使用:
    # 正则表达式以r作引导
    print(re.findall(r'\d+',text))   # 匹配数值,一位或多位
    # ['123', '15278334412', '345']
    
    pos2=re.compile(r'[a-zA-Z]+\d+')   # 定义正则表达式对象;匹配以一个或多个字母开头,一个或多个数字结尾
    print(pos2.findall(string))   # 进行字符串匹配
    # ['proudly123', 'see345']
    
    pos3=re.compile(r'[pj]ython')   # 匹配'python'或'jython'
    print(pos3.findall(string))   # findall函数,全部匹配的都输出
    # ['python', 'jython', 'python', 'python', 'python', 'python', 'python']
    
    print(re.findall(r'(?:http://)?www\.[a-zA-Z]+\.org',string1))
    print(re.findall(r'(?:http://)?www\.\w+\.org',string1))
    # 两个表达式都是匹配网页链接的
    # ['http://www.python.org', 'www.pypython.org', 'www.python.org']
    # ['http://www.python.org', 'www.pypython.org', 'www.python.org']
    
  • # 例二,正则表达式的基础使用
    import re
    
    string = '''RegExr was created -3 by gskinner.com and is proudly123 hosted by Media Temple. phone:15278334412
    Edit the 5.12 Expression & Text to see345 matches. Roll over matches or the expression for details. PCRE & JavaScript flavors of RegEx are supported. Validate your expression3.6 with -52 Tests mode.'python、jython'http://www.python.org'、'http://python.org'、www.pypython.org、'www.python.org'和'python.org"
    '''
    print(re.findall(r'-?\d+', string))   # 匹配数字
    # ['-3', '123', '15278334412', '5', '12', '345', '3', '6', '-52']
    
    print(re.findall(r'-?\d+\.\d*', string))   # 匹配小数
    # ['5.12', '3.6']
    
    print(re.findall(r'-?\d+\.\d*|-?\d+', string))   #惰性求值的特点;匹配数值以及小数
    # ['-3', '123', '15278334412', '5.12', '345', '3.6', '-52']
    
    print(re.findall(r'\d\d\d\d\d\d\d\d\d\d\d',string))   #粗浅版匹配电话号码
    print(re.findall(r'\d{11}',string))
    # ['15278334412']
    # ['15278334412']
    
1.4 子模式
  • 使用括号代表一个子模式;子模式扩展语法(简略):

  • 语法描述
    (?#…)表示注释
    (?=…)用于正则表达式之后,表示出现=号匹配的字符则匹配
    (?!..)用于正则表达式之后,表示!后的内容在字符串中不出现则匹配
    (?<=…)用于正则表达式之前,与(?=…)含义相同
    (?<!..)用于正则表达式之前,与(?!..)含义相同
1.5 复杂用法案例
  • import re
    
    str1 = '''192.168.1.2RegExr was created  by gskinner.com and is proudly123 hosted by Media. phone:15278334412张伟 1996.8.24;王芳 1997-7-24李娜 1993年1月6日
    Edit the Expression & Text 19455.5.13to see345 matches. 王伟 1993年1月2日
    Roll over李伟 1996.3.21matches or the expression for details. PCRE & JavaScript flavors of RegEx are supported. Validate your expressionwith Tests mode.'python、jython'http://www.python.org'、'http://python.org'、www.pypython.org、'www.python.org'和'python.org"
    '''
    
    # 匹配出生年月日
    print(re.findall(r'(?<!\d)\d{4}[\.,\-,年]\d{1,2}[\.,\-,月]\d{1,2}[日]?', str1))
    # ['1996.8.24', '1997-7-24', '1993年1月6日', '1993年1月2日', '1996.3.21']
    
  • #验证输入的字符串是否为合法的密码,规则:长度8-12位,由大、小写字母、数字或下划线构成,且必须大写字母开头(要求,其中要使用到match方式)
    import re
    
    str1 = input('请输入密码:')
    patter = re.compile(r'[A-Z]\w{7,11}$')  # 排除结尾是特殊字符
    if patter.match(str1):
        print('密码符合规范')
    else:
        print('不符合规范')
    
  • # 编写一个函数,要求该函数输入一个数值,并判断出该数值是否为浮点数(必须要使用正则表达式方式实现)
    import re
    
    
    def my_float():
        str1 = input('请输入一个数值:')
        patter = re.compile(r'-?\d+\.\d+$')
        if patter.findall(str1):
            print('是浮点数')
        else:
            print('不是浮点数')
    
    
    for i in range(2):   # 调用两次
        my_float()
    
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值