Python学习心得正则表达式及re模块的使用

一、正则表达式定义以及正则表达式的构成: 

1.什么式正则表达式:

正则表达式:是一个比较特殊的字符序列,能够帮助用户检查一个字符串是否符合某种特殊的模式。

2.正则表达式的构成:

正则表达式是由元字符限定字符构成的,元字符用来限定匹配的对象,限定符用来限定匹配的个数。

a.元字符:具有特殊意义的专用字符。例如:^和&分别表示开始和结束。

元字符

描述说明

举例

结果

.

匹配任意字符(除\n)

'p\nytho\tn'

p、y、t、h、o、lt、n

\w

匹配字母、数字、下划线

'python\n123'

p、y、t、h、o、n、1、2、3

\W

匹配非字母、数字、下划线

'python\n123'

\n

\s

匹配任意空白字符

'python\t123'

\t

\S

匹配任意非空白字符

'python\t123'

p、y、t、h、o、n、1、2、3

\d

匹配任意十进制数

'python\t123'

123

b. 限定符:用于限定匹配的次数。

限定符

描述说明

举例

结果

?

匹配前面的字符0次或1

colour?r

可以匹配colorcolour

+

匹配前面的字符1次或多次

colour+r

可以匹配colourcolour...r

*

匹配前面的字符0次或多次

colour*r

可以匹配colorcolour...r

{n}

匹配前面的字符n

colour{2}r

可以匹配colour

{n,}

匹配前面的字符最少n

colour{2,}r

可以匹配colourcolour...r

{n,m}

匹配前面的字符最小n次,最多m

colour{2,4}r

可以匹配colourcolour...r  

二、正则表达式的应用:

正则表达式有了,如何对其应用,下面进行解释:

对其进行应用利用了,python中的re模块,通过调用re模块从而实现其操作。

注:python中的re内置模块,可以帮助python实现其正则表达式操作。

1.re模块中的一系列函数:

应用re模块就是对这些函数进行调用

注:调用函数前需要对其进行说明'import re'。

函数

功能描述

re.match(pattern, string, flags=0)

用于从字符串的开始位置进行匹配,如果起始位置匹配成功,结果为Match对象,否则结果为None

re.search(pattern, string, flags=0)

用于在整个字符串中搜索第一个匹配的值,如果匹配成功,结果为Match对象,否则结果为None

re.findall(pattern, string, flags=0)

用于在整个字符串搜索所有符合正则表达式的值,结果是一个列表类型。

re.sub(pattern, repl, string, count, flags=0)

用于实现对字符串中指定子串的替换。

re.split(pattern, string, maxsplit, flags=0)

字符串中的split()方法功能相同,都是分隔字符串。

2.函数的调用:

(1) re.match(pattern, string, flags=0)的运用:

import re#导入
pattern='\d\.\d+'#+限定符,\d 0-9数字出现1次或多次
s='I study every day'#待匹配字符串
match=re.match(pattern,s,re.I)
print(match)#None
s2='3.11Python I study every day'
match2=re.match(pattern,s2)
print(match2)#<re.Match object; span=(0, 4), match='3.11'>

print('匹配值的起始位置:',match2.start())
print('匹配值的结束位置:',match2.end())
print('匹配区间的位置元素:',match2.span())
print('待匹配的字符串:',match2.string)
print('匹配的数据:',match2.group())

(2)re.search(pattern, string, flags=0)的运用:

import re#导入
pattern='\d\.\d+'#+限定符,\d 0-9数字出现1次或多次
s='I study Python3.11 every day'#待匹配字符串
match=re.search(pattern,s,re.I)

s2='4.10 Python I study every day'
s3='Python I study every day'
match3=re.search(pattern,s3)
match2=re.search(pattern,s2)
print(match)
print(match2)
print(match3)

print(match.group())
print(match2.group())

re.findall(pattern, string, flags=0)的运用:

import re#导入
pattern='\d\.\d+'#+限定符,\d 0-9数字出现1次或多次
s='I study Python3.11 and Python2.7 every day'#待匹配字符串
s2='4.10 Python I study every day'
s3='Python I study every day'
lst=re.findall(pattern,s)
lst2=re.findall(pattern,s2)
lst3=re.findall(pattern,s3)

print(lst)
print(lst2)
print(lst3)

(3)re.sub(pattern, repl, string, count, flags=0)和re.split(pattern, string, maxsplit, flags=0)的运用:

import re
pattern='黑客|破解|反爬|爬虫'
s='我想学习Python,想破解一些VIP视频,Python可以实现无低反爬吗?'
new_s=re.sub(pattern,'XXX',s)
print(new_s)

s2='http://www.baidu.com/s?wd=wjl&rsv_zpt=1'
pattern2='[?|&]'
lst=re.split(pattern2,s2)
print(lst)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值