Python正则表达式

一、正则表达式
在这里插入图片描述
基本语法
findall() 查找所有非重叠匹配项,返回列表list

import re
tex='Life consists 5 not in holding 6 good 8 cards. '
pattern=re.compile(r'\d+')       #加上r 确保不用转义, \d+ 找出所有数字
#1.方法一
print(pattern.findall(tex))      #.findall(文本) 找出文本中的数字

#1.方法二
print(re.findall(r'\d+',tex))     

运行结果:

['5', '6', '8']
['5', '6', '8']

Process finished with exit code 0
import re
tex='Life Consists 5 not in Holding 6 Good 8 Cards. '
pattern_1=re.compile(r'[a-z]\w+')       #加上r 确保不用转义,[a-z]\w+  查找所有的小写字母1
pattern_2=re.compile(r'[A-Z]\w+')                          #从[A-Z]中找出连续的字母
print(pattern_1.findall(tex))
print(pattern_2.findall(tex))

运行结果:

['ife', 'onsists', 'not', 'in', 'olding', 'ood', 'ards']
['Life', 'Consists', 'Holding', 'Good', 'Cards']

Process finished with exit code 0
import re
tex='hello world FW so sss '
pattern=re.compile(r'(\w+) (\w+)')   #将字符串 两个为一组分隔开 ,第五个sss 被舍弃
print(pattern.findall(tex))

运行结果:

[('hello', 'world'), ('FW', 'so')]

Process finished with exit code 0

.match 反馈查找匹配的字符的位置,仅从开始的位置匹配

import re
tex='hello world FW so sss '
pattern = re.compile(r'hell')    #从tex这个字符串里匹配hell这个字符
print(pattern.match(tex))        

运行结果:

<re.Match object; span=(0, 4), match='hell'>

Process finished with exit code 0

search()任意位置搜索

import re
tex='hello world FW so sss '
pattern = re.compile(r'wor')    
print(pattern.search(tex))

运行结果:

<re.Match object; span=(6, 9), match='wor'>

Process finished with exit code 0

finditer()查找所有匹配,返回迭代器,需要遍历

import re
tex='hello 5 world 7 6FW so 8 sss '
pattern = re.compile(r'\d+')
it=pattern.finditer(tex)
for m in it:
    print(m)

运行结果:

<re.Match object; span=(6, 7), match='5'>
<re.Match object; span=(14, 15), match='7'>
<re.Match object; span=(16, 17), match='6'>
<re.Match object; span=(23, 24), match='8'>

Process finished with exit code 0
import re
tex='hello 5 world 7 6FW so 8 sss '
pattern = re.compile(r'(\d+)')  #把一个\d+放进分组里
print(pattern.findall(tex))

pattern = re.compile(r'(\d+) (\d+)')  #把两个紧邻数字 两两匹配
print(pattern.findall(tex))

pattern = re.compile(r'(\d+).(\d+)')  #  . 所有可能性的字符
print(pattern.findall(tex))

pattern = re.compile(r'(\d+).*?(\d+)')  #  .*? 表示匹配任意数量的重复,但是在能使整个匹配成功的前提下使用最少的重复
print(pattern.findall(tex))

运行结果:

['5', '7', '6', '8']
[('7', '6')]
[('7', '6')]
[('5', '7'), ('6', '8')]
import re
text='Tom is 8 years old. Jerry is 23 years old.'
pattern=re.compile(r'(\d+).*?(\d+)')  # 8 - 23 数字之间的所有
m=pattern.search(text)
print(m.group())
print(m.group(2))
print(m.end(2))
print(m.start(1))

运行结果:

8 years old. Jerry is 23
23
31
7

Process finished with exit code 0
import re
print(re.search(r'sen(re|ce)' , 'senre')) #以sen为基础,re ce都能匹配
import re
text='Simple, 5 will 8 live the real me.'
p=re.compile(r'\n')  #通过空格分隔
print(p.split(text))

p=re.compile(r'\W')   #通过非字母分隔
print(p.split(text))

ors='Simple, 5 will 8 live the real me.'   #把数字 替换成 +号
print(re.sub(r'\d+', '+', ors))

运行结果:

['Simple, 5 will 8 live the real me.']
['Simple', '', '5', 'will', '8', 'live', 'the', 'real', 'me', '']
Simple, + will + live the real me.

Process finished with exit code 0

在这里插入图片描述

import re
tex='Python PYTHON python'
print(re.search(r'python',tex))
print(re.findall(r'python',tex,re.I))  #忽略大小写,找到全部
print(re.findall(r'^hello','\nhello',re.M))  #在每一行都找

运行结果:

<re.Match object; span=(14, 20), match='python'>
['Python', 'PYTHON', 'python']
['hello']
Process finished with exit code 0

re.escape(pattern) 可以对字符串中所有可能被解释为正则运算符的字符进行转义的应用函数。如果字符串很长且包含很多特殊技字符,而你又不想输入一大堆反斜杠,或者字符串来自于用户(比如通过raw_input函数获取输入的内容),且要用作正则表达式的一部分的时候,可以使用这个函数。

import re
print(re.findall(re.escape('^'),'^python^'))  #忽略位置信息

运行结果:

['^', '^']

Process finished with exit code 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值