目录
  • 一、re是什么?
  • 二、re 模块的作用
  • 三、re模块的使用
    • 1、常用方法
    • 2、 元字符
    • 3、多字符匹配
    • 4、分组匹配
    • 5、match()方法的使用
    • 6、 search():在字符串中搜索,返回搜索到的第一个
    • 7、 finditer():
  • 总结

    一、re是什么?

    正则表达式是一个特殊的字符序列,能方便的检查一个字符串是否与某种模式匹配。re模块使得python拥有全部的正则表达式功能。

    二、re 模块的作用

    通过使用正则表达式,可以:
    测试字符串内的模式。—— 例如,可以测试输入字符串,以查看字符串内是否出现电话号码模式或信用卡号码模式。这称为数据验证。
    替换文本。—— 可以使用正则表达式来识别文档中的特定文本,完全删除该文本或者用其他文本替换它。
    基于模式匹配从字符串中提取子字符串。—— 可以查找文档内或输入域内特定的文本。

    三、re模块的使用

    1、常用方法

    • findall(): 匹配所有的字符串,把匹配结果作为一个列表返回
    • match(): 匹配字符串的开始位置,如果开始位置没有,则返回none
    • search():在字符串中搜索,返回搜索到的第一个
    • finditer():匹配所有的字符串,返回迭代器

    2、 元字符

    匹配任意字符(除\n以外) h. 代表匹配h后的任意一个字符

    import re
    res = 'h.'
    s = 'hello python'
    result = re.findall(res, s)
    print(result)  # ['he', 'ho']
    

    [] 拿[]中的人任意一个字符,去字符串中匹配,匹配到一个返回一个,最后以列表返回

    import re
    res2 = '[hon]'
    s = 'hello python'
    result = re.findall(res2, s)
    print(result)  # ['h', 'o', 'h', 'o', 'n']
    

    \d 匹配数字0-9

    import re
    res2 = '[\d]'
    s = 'hell666o pyt999hon'
    result = re.findall(res2, s)
    print(result)  # ['6', '6', '6', '9', '9', '9']
    

    \d 匹配非数字, 包含空格

    import re
    res2 = '[\d]'
    s = 'hello 3334 python 88'
    result = re.findall(res2, s)
    print(result)  # ['h', 'e', 'l', 'l', 'o', ' ', ' ', 'p', 'y', 't', 'h', 'o', 'n', ' ']
    

    ‘\s’ 匹配空白字符

    import re
    res2 = '[\s]'
    s = 'hello 3334 python 88'
    result = re.findall(res2, s)
    print(result)  # [' ', ' ', ' ']
    

    ‘\s’ 匹配非空白字符

    import re
    res2 = '[\s]'
    s = 'hello 3334 python 88'
    result = re.findall(res2, s)
    print(result)  # ['h', 'e', 'l', 'l', 'o', '3', '3', '3', '4', 'p', 'y', 't', 'h', 'o', 'n', '8', '8']
    

    \w 匹配非特殊字符,即a-z、a-z、0-9、_、汉字

    import re
    res2 = '[\w]'
    s = 'hello#&_ aa 8python中国'
    result = re.findall(res2, s)
    print(result)  # ['h', 'e', 'l', 'l', 'o', '_', 'a', 'a', '8', 'p', 'y', 't', 'h', 'o', 'n', '中', '国']
    

    \w 匹配特殊字符 ( – ~@#$&*)空格也属于特殊字符

    import re
    res2 = '[\w]'
    s = '-hello#&_ aa 8python中国'
    result = re.findall(res2, s)
    print(result)  # ['-', '#', '&', ' ', ' ']
    

    3、多字符匹配

    (1)*:匹配前一个字符出现一次,或无限次 贪婪模式

    import reres2 = 'h*'s = '-hhello hhh python'result = re.findall(res2, s)print(result)  #['', 'hh', '', '', '', '', '', 'hhh', '', '', '', '', 'h', '', '', '']import re
    res2 = 'h*'
    s = '-hhello hhh python'
    result = re.findall(res2, s)
    print(result)  #['', 'hh', '', '', '', '', '', 'hhh', '', '', '', '', 'h', '', '', '']
    

    (2) + :匹配前一个字符出现1次或无穷次

    import re
    res2 = 'h+'
    s = '-hhello hhh python'
    result = re.findall(res2, s)
    print(result) # ['hh', 'hhh', 'h']
    

    (3)?: 匹配前一个字符出现0次或者1次,非贪婪模式

    import re
    res2 = 'h?'
    s = '-hhello hhh python'
    result = re.findall(res2, s)
    print(result) # ['', 'h', 'h', '', '', '', '', '', 'h', 'h', 'h', '', '', '', '', 'h', '', '', '']
    

    (4) {n} :匹配前一个字符连续出现n次

    import re
    res2 = 'https{2}'
    s = '-hhello-httpssss-python'
    result = re.findall(res2, s)
    print(result) # ['httpss'] 
    匹配到前一个字符s 连续出现2次
    

    {n,m} :匹配前一个字符出现n-m次

    import re
    res2 = 'https{1,3}'
    s = '-hhello-httpssss-python'
    result = re.findall(res2, s)
    print(result) # ['httpss']
    

    (5) 贪婪模式和非贪婪模式

    正则表达式通常使用于查找匹配字符串。贪婪模式,总是尝试匹配尽可能多的字符;非贪婪模式正好相反,总是尝试匹配尽可能少的字符。在”*”,”?”,”+”,”{m,n}”后面加上?,使贪婪变成非贪婪。

    (6) | :两个条件进行匹配,或的关系

    import re
    res2 = 'he|ll'
    s = 'hello python'
    result = re.findall(res2, s)
    print(result) # ['he', 'll']
    

    (7)边界值:

    ^ :匹配以哪个字符开头的

    import re
    res2 = '^he'
    s = 'hello python'
    result = re.findall(res2, s)
    print(result) # ['he']
    

    $ : 匹配以哪个字符结尾的字符

    import re
    res2 = 'on$'
    s = 'hello python'
    result = re.findall(res2, s)
    print(result) # ['on']
    

    4、分组匹配

    () :只匹配()里面的

    import re
    res2 = '#(\w.+?)#'
    s = "{'mobile_phone':'#mobile_phone#','pwd':'aa123456'}"
    result = re.findall(res2, s)
    print(result)  # ['mobile_phone']
    

    5、match()方法的使用

    str = "www.runoob.com"
    print(re.match('www', str).span())  # 在起始位置匹配 ,返回匹配到的区间下标  (0,3)
    print(re.match('com', str))  # 不在起始位置匹配  none
    

    6、 search():在字符串中搜索,返回搜索到的第一个

    str = "www.runoob.com"
    print(re.search('www', str).span())  # 在起始位置匹配 ,返回匹配到的区间下标
    print(re.search('com', str).span())  # 不在起始位置匹配
    

    re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回none;而re.search匹配整个字符串,直到找到一个匹配。

    7、 finditer():

    匹配所有的字符串,返回迭代器和 findall 类似,在字符串中找到正则表达式所匹配的所有子串,并把它们作为一个迭代器返回。

    res = 'h.'
    s = 'hello python'
    result = re.finditer(res, s)
    for str in result:
        print(str.group())
    he
    ho
    

    总结

    本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注www.887551.com的更多内容!