Python正则表达式,这一篇就够了

Python正则表达式

python中re模块提供了正则表达式的功能,常用的有四个方法(match、search、findall)都可以用于匹配字符串

match

匹配字符串:

"""
re.match()必须从字符串开头匹配match方法尝试从字符串的起始位置
匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。
主要参数如下:
re.match(pattern, string)
pattern     匹配的正则表达式
string      要匹配的字符串
"""

​
# 例如:
import re
a = re.match('hello', 'helloworld')
print(a)
print(a.group())
print(a.span())
print(a.start())
print(re.match('hello', 'ahelloworld'))
​
# 输出结果如下:
<re.Match object; span=(0, 4), match='hello'>
hello
(0, 5)
0
None
"""
从例子中我们可以看出,re.match()方法返回一个匹配的对象,
而不是匹配的内容。如果需要返回内容则需要调用group()。
通过调用span()可以获得匹配结果的位置。而如果从起始位
置开始没有匹配成功,即便其他部分包含需要匹配的内容,
re.match()也会返回None。
"""

单字符匹配:

# 以下字符,都匹配单个字符数据。且开头(从字符串0位置开始)没匹配到,即使字符串其他部分包含需要匹配的内容,.match也会返回none
字符 功能
. 匹配任意一个字符(除了\n)
[] 匹配[]中列举的字符
\d 匹配数字,即0~9
\D 匹配非数字,即不是数字
\s 匹配空白,即空格,tab键
\S 匹配非空白
\w 匹配单词字符,即a~z A~Z 0-9 _
\W 匹配非单词字符
  • . 匹配任意一个字符(除了\n)
import re
a = re.match('..', 'helloworld')
print(a.group())     
# 输出 he
b = re.match('ab.', 'helloworld')
print(b)     
# 输出none,因为正则表达式是以固定的ab开头,然后跟上通配符,
# 所以必须要先匹配上ab,才会往后进行匹配
  • \d 匹配数字
"""
一个\d代表一个数字。开头没匹配到,即使字符串其他部分
包含需要匹配的内容,.match也会返回none
"""
import re
a = re.match('\d\d', '12helloworld')
print(a.group())           
# 输出为:<re.Match object; span=(0, 2), match='12'>
b = re.match('\d\d\d', '12helloworld')
print(b)                  
# 输出为:none
c = re.match('\d', 'helloworld')
print(c)                  
# 输出为:none
  • \D 匹配非数字

# 开头没匹配到,即使字符串其他部分包含需要匹配的内容,.match也会返回none
import re
a = re.match('\D', '12helloworld')
print(a)            
# 输出为:none
b = re.match('\D\D','*helloworld')
print(b)            
# 输出为:<re.Match object; span=(0, 2), match='*h'>
  • \s 匹配特殊字符,如空白,空格,tab等
import re
print(re.match('\s', ' 23he  helloworld'))      
#匹配空格,输出为:<re.Match object; span=(0, 1), match=' '>
print(re.match('\s', '  23hel  helloworld'))    
#匹配tab键,输出为:<re.Match object;
  • 21
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值