python自动化之re模块详解

作者:FamilyYan 时间:2021-07-01 19:34:16 

一、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

来源:https://blog.csdn.net/qq_37982823/article/details/122489662

标签:python,自动化,re模块
0
投稿

猜你喜欢

  • Ubuntu下使用Python实现游戏制作中的切分图片功能

    2021-02-22 22:55:53
  • 我放弃Python转Go语言的9大理由(附优秀书籍推荐)

    2022-05-27 04:46:17
  • python计算程序开始到程序结束的运行时间和程序运行的CPU时间

    2023-08-04 02:11:09
  • Python实现简单的四则运算计算器

    2022-12-29 20:44:08
  • 利用Python将多张图片合成视频的实现

    2022-09-16 07:10:26
  • PHP中常用的数组操作方法笔记整理

    2023-06-06 15:17:12
  • Linux 下 Python 实现按任意键退出的实现方法

    2022-08-07 14:22:01
  • 深入浅析Python中的yield关键字

    2022-04-18 05:02:32
  • php简单实现批量上传图片的方法

    2023-11-20 13:55:53
  • 一个jquery日期选取插件源码

    2009-12-23 19:15:00
  • python单线程文件传输的实例(C/S)

    2023-04-07 22:45:48
  • python 2.7.13 安装配置方法图文教程

    2023-11-13 11:54:37
  • python爬虫 urllib模块反爬虫机制UA详解

    2022-04-07 02:22:51
  • 基于Python pyecharts实现多种图例代码解析

    2021-11-10 05:20:17
  • python列表数据增加和删除的具体实例

    2021-08-05 15:11:09
  • Python实现方便使用的级联进度信息实例

    2021-04-22 12:31:18
  • 用ASP判断客户端浏览器语言自动跳转

    2010-07-09 13:34:00
  • PHP垃圾回收机制超详细介绍

    2023-11-21 23:11:44
  • Python程序员开发中常犯的10个错误

    2023-12-14 04:56:32
  • 用server.transfer隐藏网页真实地址

    2007-12-04 13:02:00
  • asp之家 网络编程 m.aspxhome.com