python自动化的re模块怎么用

发布时间:2022-01-15 16:05:21 作者:iii
来源:亿速云 阅读:147

今天小编给大家分享一下python自动化的re模块怎么用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

一、re是什么?

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

二、re 模块的作用

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

三、re模块的使用

1、常用方法

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

以上就是“python自动化的re模块怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

推荐阅读:
  1. python正则re模块
  2. python re模块

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python re

上一篇:WEB 版的报表工具的意义是什么

下一篇:springboot整合quartz定时任务框架的方法是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》