通常用于对一段文本进行替换、检索或提取等操作

    1. import re

    示例
    对输入的邮箱值进行校验

    python正则表达式 - 图1

    1. import re
    2. regex = r'^[0-9a-z]+\w*@([0-9a-z]+\.)+[0-9a-z]+$'
    3. email = 'luBan2019@sina.com'
    4. matchObj = re.match(regex, email)
    5. if matchObj:
    6. print('匹配的内容:' + matchObj.group())
    7. else:
    8. print("没有匹配的内容")

    \s 匹配任何空白字符g \S匹配任何非空白字符

    使用字符表达式 regex = r’^+86$’ 校验字符串的开头和结尾

    ^ 表示字符串的开头 ¥$ 表示字符串的结尾

    • 是因为加号也是一个表达式 +表示加号本身

    单词字符相关表达式

    python正则表达式 - 图2

    数字相关表达式

    python正则表达式 - 图3

    点表达式

    python正则表达式 - 图4

    逻辑或

    regex = r’^13|14|15|16|17|18|19’

    子表达式

    regex = r’^(13(4|5|6|7|8|9)$)’ 表示有着相同的前缀,不同的只是第三个数字 regex = r’[1]‘ 表示只能是数字 英文字母大小写开头

    排除范围

    在中括号内,用^来表示排除范围

    限定符表达式

    可限定出现的次数 python正则表达式 - 图5 python正则表达式 - 图6

    非贪婪模式
    在一般限定符或范围限定符后面加?
    单次检索

    1. re.search(regex, text) '检索全文,返回第一个'
    2. re.match(regex, text) '检查文本的开头'
    3. matchObj.group() '调用结果的对象'

    索引

    1. matchObj.start() '调用返回对象的索引 从0开始计算 取第一个结果对象的首字符'
    2. matchObj.end() '从1开始计算 取第一个结果的末字符 '
    3. matchObj.span() '返回元组 包含两个数据'

    全量检索

    1. re.findall(regex, text) 返回列表

    迭代器

    1. import re
    2. line = 'I love dogs cats and others'
    3. regex = r'\w+\s'
    4. matchObjs = re.finditer(regex, line)
    5. for matchObj in matchObjs :
    6. print(matchObj.group() + ' start=' + str(matchObj.start()) + ' end=' + str(matchObj.end()))
    7. #所有结果
    8. I start=0 end=2
    9. love start=2 end=7
    10. dogs start=7 end=12
    11. cats start=12 end=17
    12. and start=17 end=21

    替换

    1. re.sub(pattern, repl, sourceText, count) 'count是最大替换次数,可以不写,不写默认全部替换'
    2. '正则表达式,替换后字符,源文件'
    1. import re
    2. text = "2019-11-23"
    3. ## 替换所有的非数字的字符,兼容性好
    4. regex = r'\D'
    5. ## 全部替换
    6. targetText = re.sub(regex, '/', text)
    7. print('日期:' + targetText)

    预编译

    1. re.compile(regex) 返回值是pattern对象
    2. import re
    3. text = "2019-11-23"
    4. ## 替换所有的非数字的字符,兼容性好
    5. regex = r'\D'
    6. ## 编译一次
    7. pattern = re.compile(regex)
    8. matchObjs = pattern.findall(text)
    9. print(matchObjs)
    10. targetText = pattern.sub('/', text)
    11. print('日期:' + targetText)

    分组
    对于返回的结果对象调用分组的方法
    1.返回整体结果 matchObj.group()
    2.指定分组 接受正整数参数 group(1)第一个子表达式
    3.全部分组 groups() 返回一个元组 包含所有分组的字符串

    实战
    网页爬取
    提取新闻链接

    1. import re
    2. import requests
    3. ## 抓取页面
    4. response = requests.get('https://news.sina.com.cn/')
    5. content = response.text
    6. print(content)
    7. ## 扫描 <a>xxx</a> 标签,这是新闻链接
    8. aTagRegex = r"<a.+?/a>"
    9. aTagPattern = re.compile(aTagRegex);
    10. matchs = aTagPattern.findall(content)
    11. for matchStr in matchs:
    12. print(matchStr)
    1. import re
    2. import requests
    3. ## 抓取页面
    4. response = requests.get('https://news.sina.com.cn/')
    5. response.encoding = 'utf-8'
    6. content = response.text
    7. ## 扫描 <a>xxx</a> 标签,这是新闻链接
    8. aTagRegex = r"<a.+?/a>"
    9. aTagPattern = re.compile(aTagRegex);
    10. ## 网址正则表达式
    11. linkRegex = r"href=\"(.+?)\""
    12. linkPattern = re.compile(linkRegex);
    13. ## 标题正则表达式
    14. titleRegex = r">(.+)<"
    15. titlePattern = re.compile(titleRegex);
    16. matchs = aTagPattern.findall(content)
    17. for matchStr in matchs:
    18. print(matchStr)
    19. ## 解析网址
    20. matchLinkObj = linkPattern.search(matchStr)
    21. if matchLinkObj :
    22. print(matchLinkObj.groups()[0])
    23. ## 解析标题
    24. matchTitleObj = titlePattern.search(matchStr)
    25. if matchTitleObj :
    26. print(matchTitleObj.groups()[0])
    27. print('')

    1. a-zA-Z0-9 ↩︎