Python正则表达式
2021/12/14 20:19:08
本文主要是介绍Python正则表达式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一.实例
import re NameRe = re.compile(r'\d{3}-\d{3}-\d{4}') #创建匹配对象,r为原始字符 name = NameRe.search("asdasdsadasd") #匹配失败返回None,成功返回Match对象,就一个 print(name.group()) #返回匹配到的内容 #直接过滤 result = re.match('You', 'Young Frankenstein') youpattern = re.compile('You') result = youpattern.match('Young Frankenstein') NameRe = re.compile(r'(\d{3})-(\d{3}-\d{4})') #分组 print(name.group(1)) #1返回1组,0和不写,返回所有匹配到的 a,b = mo.groups() #分别获得变量 name = NameRe.findall('asdasdas') #返回所有匹配结果,列表套元组 re.complir(r'abc|bcd') #匹配其中一个,返回第一次匹配到的值 re.compile(r'Bat(man|mobile|copter|bat)') #Bat可以复写 re.compile(r'Bat(wo)?man') #wo可有可无,匹配问好前的分组0次或1次 re.compile(r'Bat(wo)*man') #匹配之前的分组0次或无数次 re.compile(r'Bat(wo)+man') #之前的分组1次或多次 re.compile(r'Bat(wo){3}') #之前的分组3次 re.compile(r'Bat(wo){1,3}') #之前的分组1次或3次 re.compile(r'Bat(wo){3,5}?') #在分组的时候,实现非贪心匹配,进来匹配最短 re.compile(r'Bat[0-5]') #0-5之前 re.compile(r'[a-zA-Z0-9]') #匹配所有字母和数字 re.compile(r'Bat[^0-5]') #取反 re.compile(r'^Bat&') #开始到结束都是Dat的 re.compile(r'.Bat') #除了换行之外的所有字符 re.compile(r'.Bat(.*)') #匹配所有字符串 re.compile(r'.Bat(.*)?') #匹配所有字符串,非贪婪匹配,尽量少匹配 re.compile('.*', re.DOTALL) #匹配所有,包括有换行等等 re.compile(r'robocop', re.I) #不区分大小写匹配 name.sub(字符,字符) #前一个是替换后的,后一个是要匹配替换的字符串 name.sub(r'\1****', 'Agentasdasd') #将第一组替换为* \2第二组 re.compile(r'''asda asdasd asdad''', re.VERBOSE) #多行正则,可写注释 re.compile('foo', re.IGNORECASE | re.DOTALL) #多参数调用
二.规则
三.具体例子
#匹配电话号码 phoneRegex = re.compile(r'''( (\d{3}|\(\d{3}\))? #3个数字或括号中的3个数字 (\s|-|\.)? #空白或-或. (\d{3}) #3个数字 (\s|-|\.) #空白或-或. (\d{4}) #4个数字 (\s*(ext|x|ext.)\s*(\d{2,5}))? #任意数量空白+可选ext+任意数量空白+2-5个数字 )''', re.VERBOSE)
#匹配邮件地址 emailRegex = re.compile(r'''( [a-zA-Z0-9._%+-]+ #一个或多个字符,字母数字下划线,百分号,加好,短横,句号 @ #@符 [a-zA-Z0-9.-]+ #同上匹配 (\.[a-zA-Z]{2,4}) #.com这种域名部分,重复2-4个 )''', re.VERBOSE)
from collections import namedtuple Duck = namedtuple('Duck', 'bill tail') duck = Duck('wide irange', 'log') duck duck.bill parts = {'bill': 'wide orange', 'tail': 'long'} #字典构造命名元祖 duck2 = Duck(**parts) duck3 = duck2._replace(tail='magnificent', bill='crushing') #替换部分值,返回新的 search() 会返回第一次成功匹配,如果存在的话 m = re.search('Frank', 'Young Frankenstein') findall() 会返回所有不重叠的匹配的列表,如果存在的话 m = re.findall('n', 'Young Frankenstein') split() 会根据 pattern 将 source 切分成若干段,返回由这些片段组成的列表; m = re.split('n', source) #以n分隔,将其它字符放到数组 sub() 还需一个额外的参数 replacement,它会把 source 中所有匹配的 pattern 改成replacement。 m = re.sub('n', '?', source) #返回字符串,将匹配到的n换成问号 #字符串常量 import string printable = string.printable print(printable) re.findall('\d', printable) #找出数字 re.findall('\w', printable) #数字,字符,下划线 re.findall('\s', printable) #哪些是空格 re.findall('[wf]ish', source) #w或f开头 re.findall('[wsh]+', source) #若干个w、s、h的组合 re.findall('I (?=wish)', source) #I开头,后面跟wish,出现次数尽量少 re.findall('(?<=I) wish', source) #查询以wish结尾,前面为I的匹配(I出现的次数尽量少)
这篇关于Python正则表达式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python