首页 / 正则 / python——正则的使用re
python——正则的使用re
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python——正则的使用re,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2055字,纯文字阅读大概需要3分钟。
内容图文
![python——正则的使用re](/upload/InfoBanner/zyjiaocheng/621/9ba915ac773440cdaeca0c8138364000.jpg)
该篇主要介绍re的使用。
1、re.compile函数
re模块中的重要函数。该函数根据包含的正则表达式的字符串创建模式对象。可以实现更有效率的匹配。在直接使用字符串表示的正则表达式进行search,match和findall操作时,python会将字符串转换为正则表达式对象。而使用compile完成一次转换后,在每次使用模式的时候就不用重复转换。当然,使用re.compile()函数进行转换后,re.search(pattern, string)的调用方式就转换为pattern.search(string)的调用方式。
import re
some_text = 'a,b,,,,c d'
reObj = re.compile('[, ]+')
reObj.split(some_text)
# ['a', 'b', 'c', 'd']
2、re.findall函数
定义如下。发返回string中所有与pattern匹配的全部字符串,返回形式为数组。
def findall(pattern, string, flags=0):
"""Return a list of all non-overlapping matches in the string.
If one or more capturing groups are present in the pattern, return
a list of groups; this will be a list of tuples if the pattern
has more than one group.
Empty matches are included in the result."""
return _compile(pattern, flags).findall(string)
findall的两种表示形式
import re
kk = re.compile(r'\d+')
print(kk.findall('one1two2three3four4'))
# [1,2,3,4]
# 注意此处findall的用法,可传两个参数:
kk = re.compile(r'\d+')
print(re.findall(kk,"one123"))
# [1,2,3]
(1)re,findall中的正则表达式(.*?)
字符串
import re
str = 'aabbabaabbaa'
print(re.findall(r'a.b', str)) # . 匹配除\n以外的任意一个字符
# ['aab', 'aab']
print(re.findall(r'a*b', str)) # * 前面的字符出现0次或以上
# ['aab', 'b', 'ab', 'aab', 'b']
print(re.findall(r'a.*b', str)) # .* 贪婪,匹配从.*前面为开始到后面为结束的所有内容
# ['aabbabaabb']
print(re.findall(r'a.*?b',str)) # 非贪婪,遇到开始和结束就进行截取,因此截取多次符合的结果,中间没有字符也会被截取
# ['aab', 'ab', 'aab']
print(re.findall(r'a(.*?)b', str)) # 符号(.*?)非贪婪,与上面一样,只是与上面的相比多了一个括号,只保留括号的内容
# ['a', '','a']
(2)re,findall中的参数re.S
import re
str = '''aabbab
aabbaa
bb'''
print(re.findall(r'a.*?b', str)) # 参数无re.S,没有把最后一个换行的aab算进来
# ['aab', 'ab', 'aab']
print(re.findall(r'a.*?b', str, re.S))
# ['aab', 'ab', 'aab', 'aa\n\t\t b'] # 参数有re.S,不会对\n进行中断
内容总结
以上是互联网集市为您收集整理的python——正则的使用re全部内容,希望文章能够帮你解决python——正则的使用re所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。