加入收藏 | 设为首页 | 会员中心 | 我要投稿 焦作站长网 (https://www.0391zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 移动互联 > 通讯 > 正文

学会Python正则表达式,就看这20个例子 脚本之家修正版

发布时间:2020-11-24 15:24:34 所属栏目:通讯 来源:互联网
导读:这篇文章主要介绍了学会Python正则表达式,就看这20个例子,需要的朋友可以参考下

  短视频,自媒体,达人种草一站服务

学会Python正则表达式,就看这20个例子(脚本之家修正版)

正则表达式是处理字符串的强大工具。作为一个概念而言,正则表达式对于Python来说并不是独有的。但是,Python中的正则表达式在实际使用过程中还是有一些细小的差别。

正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配。

Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式。

re 模块使 Python 语言拥有全部的正则表达式功能。

compile 函数根据一个模式字符串和可选的标志参数生成一个正则表达式对象。该对象拥有一系列方法用于正则表达式匹配和替换。

re 模块也提供了与这些方法功能完全一致的函数,这些函数使用一个模式字符串做为它们的第一个参数。

1、查找第一个匹配串

import re
s='i love python very much'
pat='python'
r=re.search(pat,s)
print(r.span())#(7,13)

2、查找所有1

import re
s='山东省潍坊市青州第1中学高三1班'
pat='1'
r=re.finditer(pat,s)
for i in r:
print(i)

# <re.Match object; span=(9, 10), match='1'>
# <re.Match object; span=(14, 15), match='1'>

3、d匹配数字[0-9]

import re
s='一共20行代码运行时间13.59s'
pat=r'd+'#+表示匹配数字(d表示数字的通用字符)1次或多次
r=re.findall(pat,s)
print(r)
#['20','13','59']

我们想保留13.59而不是分开,请看4

4、?表示前一个字符匹配0或1次

import re

s='一共20行代码运行时间13.59s'
pat=r'd+.?d+'#?表示匹配小数点(.)0次或1次
r=re.findall(pat,s)
print(r)
#['20','13.59']

5、^匹配字符串的开头

import re

s='This module provides regular expression matching operations similar to those found in Perl'
pat=r'^[emrt]' #查找以
r=re.findall(pat,s)
print(r)
# [],因为字符串的开头是字符`T`,不在emrt匹配范围内,所以返回为空

6、re.I 忽略大小写

import re

s='This module provides regular expression matching operations similar to those found in Perl'
pat=r'^[emrt]' #查找以
r=re.compile(pat,re.I).search(s)
print(r)
# <re.Match object; span=(0, 1), match='T'> 表明字符串的开头在匹配列表中

7、使用正则提取单词

这是不准确版本,请参看第9个

import re

s='This module provides regular expression matching operations similar to those found in Perl'
pat=r's[a-zA-Z]+'
r=re.findall(pat,s)
print(r) #[' module', ' provides', ' regular', ' expression', ' matching', ' operations', ' similar', ' to', ' those', ' found', ' in', ' Perl']

8、只捕获单词,去掉空格

使用()捕获,这是不准确版本,请参看第9个

import re

s='This module provides regular expression matching operations similar to those found in Perl'
pat=r's([a-zA-Z]+)'
r=re.findall(pat,s)
print(r)
#['module', 'provides', 'regular', 'expression', 'matching', 'operations', 'similar', 'to', 'those', 'found', 'in', 'Perl']

9、补充上第一个单词

上面第8,看到提取单词中未包括第一个单词,使用?表示前面字符出现0次或1次,但是此字符还有表示贪心或非贪心匹配含义,使用时要谨慎。

import re

s='This module provides regular expression matching operations similar to those found in Perl'
pat=r's?([a-zA-Z]+)'
r=re.findall(pat,s)
print(r)
#['This', 'module', 'provides', 'regular', 'expression', 'matching', 'operations', 'similar', 'to', 'those', 'found', 'in', 'Perl']

10、使用split函数直接分割单词

使用以上方法分割单词,不是简洁的,仅仅为了演示。分割单词最简单还是使用split函数。

import re

s = 'This module provides regular expression matching operations similar to those found in Perl'
pat = r's+'
r = re.split(pat,s)
print(r)
#['This', 'module', 'provides', 'regular', 'expression', 'matching', 'operations', 'similar', 'to', 'those', 'found', 'in', 'Perl']

11、提取以m或t开头的单词,忽略大小写

下面出现的结果不是我们想要的,原因出在 ?上!

import re

s='This module provides regular expression matching operations similar to those found in Perl'
pat=r's?([mt][a-zA-Z]*)' # 查找以
r=re.findall(pat,s)
print(r)
#['module', 'matching', 'tions', 'milar', 'to', 'those']

12、使用^查找字符串开头的单词

综合11和12得到所有以m或t开头的单词

import re

s='This module provides regular expression matching operations similar to those found in Perl'
pat=r'^([mt][a-zA-Z]*)s' # 查找以
r=re.compile(pat,re.I).findall(s)
print(r)
#['This']

13、先分割,再查找满足要求的单词

使用match表示是否匹配

import re

(编辑:焦作站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读