Python正则表达式

正则表达式基础语法(经常使用)

  • 第一类
    • ^ 匹配字符串的开头
    • $ 匹配字符串的末尾。
    • . 匹配任意字符,除了换行符,当re.DOTALL标记被指定时,则能够匹配包括换行符的任意字符。
    • [...] 用来表示一组字符,单独列出:[amk] 匹配 'a','m'或'k'
    • [^...] 不在[]中的字符:[^abc] 匹配除了a,b,c以外的字符。
    • re* 匹配0个或多个的表达式。
    • re+ 匹配1个或多个的表达式。
    • re? 匹配0个或1个由前面的正则表达式定义的片断,非贪婪方式
    • re{ n} 精确匹配 n 个前面表达式。例如, o{2} 不能匹配 "Bob" 中的 "o",可是能匹配 "food" 中的两个 o。
    • re{ n,} 匹配 n 个前面表达式。例如, o{2,} 不能匹配"Bob"中的"o",但能匹配 "foooood"中的全部 o。"o{1,}" 等价于 "o+"。"o{0,}" 则等价于 "o*"。
    • re{ n, m} 匹配 n 到 m 次由前面的正则表达式定义的片断,贪婪方式
    • a| b 匹配a或b
  • 第二类
    • \w 匹配字母数字及下划线
    • \W 匹配非字母数字及下划线
    • \s 匹配任意空白字符,等价于 [\t\n\r\f].
    • \S 匹配任意非空字符
    • \d 匹配任意数字,等价于 [0-9].
    • \D 匹配任意非数字

Python正则——re库

  • re.match(pattern, string, flags=0) 从字符串的起始位置匹配一个模式,若是不是起始位置匹配成功的话,match()就返回none正则表达式

    • pattern 匹配的正则表达式
    • string 要匹配的字符串
    • 标志位,用于控制正则表达式的匹配方式
      • re.I 使匹配对大小写不敏感
      • re.L 作本地化识别(locale-aware)匹配
      • re.M 多行匹配,影响 ^ 和 $
      • re.S 使 . 匹配包括换行在内的全部字符
      • re.U 根据Unicode字符集解析字符。这个标志影响 \w, \W, \b, \B.
      • re.X 该标志经过给予你更灵活的格式以便你将正则表达式写得更易于理解。
    def test_match():
        string = "hello world meili fengxin hello"
        match = re.match(r"hello", string)
        if match:
            print(match.group(0))
  • re.search(pattern, string, flags=0) 扫描整个字符串并返回第一个成功的匹配app

    def test_search():
        string = "world meili fengxin hello"
        match = re.search(r'hello', string)
        if match:
            print(match.group(0))
  • re.split(pattern, string[, maxsplit=0, flags=0])按照可以匹配的子串将字符串分割后返回列表函数

    • pattern 匹配的正则表达式
    • string 要匹配的字符串。
    • maxsplit 分隔次数,maxsplit=1 分隔一次,默认为 0,不限制次数。
    • flags 标志位
    def test_split():
        string = "hello world meili fengxin hello"
        li = re.split(r" ", string)
        for ele in li:
            print(ele)
  • re.sub(pattern, repl, string, count=0, flags=0) 用于替换字符串中的匹配项ui

    • pattern : 正则中的模式字符串。
    • repl : 替换的字符串,也可为一个函数。
    • string : 要被查找替换的原始字符串。
    • count : 模式匹配后替换的最大次数,默认 0 表示替换全部的匹配。
    def test_sub():
        string = "hello world meili fengxin hello"
        new_str = re.sub(r"hello", "你好", string)
        print(new_str)
  • findall(pattern,string,pos,endpos) 在字符串中找到正则表达式所匹配的全部子串,并返回一个列表,若是没有找到匹配的,则返回空列表spa

    def test_findall():
        string = "hello world meili fengxin hello"
        li = re.findall(r"hello", string)
        print(li)
  • re.finditer 在字符串中找到正则表达式所匹配的全部子串,并把它们做为一个迭代器返回code

    def test_finditer():
        string = "hello world meili fengxin hello hello"
        it = re.finditer(r"hello", string)
        for match in it:
            print(match.group())

re.complie

compile 函数用于编译正则表达式,生成一个正则表达式( Pattern )对象,供 match()、search()、split()等上述李哥函数使用。orm

  • 语法格式为:re.compile(pattern[, flags])
    • pattern : 一个字符串形式的正则表达式
    • flags :可选,表示匹配模式,好比忽略大小写,多行模式等。
  • 可直接调用上述6个方法,去掉第一个参数便可
    # -*- coding: UTF-8 -*-
    
    import re
    
    str = "hello world nihao shijie hello"
    
    regObj = re.compile("hello")
    match = regObj.match(str)
    if match:
        print(match.group(0))