正则表达式之 findall 和 search 和 sub 方法

##########################################################

# 正则表达式之 findall 和 search 和 sub 方法
import re
# 创建一个 Regex 对象
phoneNumRegex = re.compile(r'\d{3}-\d{3}-\d{4}') # 匹配三个数字-三个数字-四个数字
mo1 = phoneNumRegex.findall('Cell:415-555-9999 Work: 212-555-0000')
print(mo1)  # 返回的是字符串列表

phoneNumRegex = re.compile(r'(\d{3})-(\d{3}-\d{4})')
mo2 = phoneNumRegex.findall('Cell:415-555-9999 Work: 212-555-0000')
print(mo2)  # 返回的是字符串元组列表

vowelRegex = re.compile(r'[aeiouAEIOU]') # 匹配所有的元音字符
mo3 = vowelRegex.findall('RoboCop eats baby food.BABY FOOD.')
print(mo3)  # 返回的是字符串列表

vowelRegex = re.compile(r'[^aeiouAEIOU]') # 匹配所有的非元音字符
mo4 = vowelRegex.findall('RoboCop eats baby food.BABY FOOD.')
print(mo4)  # 返回的是字符串列表

vowelRegex = re.compile(r'[a-zA-Z0-9]')
mo5 = vowelRegex.findall('RoboCop5 eats baby2 food.BABY FOOD.')
print(mo5)  # 返回的是字符串列表

vowelRegex = re.compile(r'^hello') # 匹配以hello开头的字符串
mo6 = vowelRegex.findall('hello world!')
print(mo6)  # 返回的是字符串列表
mo6 = vowelRegex.findall('He said hello!')
print(mo6)  # 返回的是空列表,因为不是以hello开头!!!

vowelRegex = re.compile(r'\d$') # 匹配以数字结尾
mo7 = vowelRegex.findall('Your number is 42')
print(mo7)  # 返回最后一个数字字符串的列表

vowelRegex = re.compile(r'^\d+$') # 匹配从开头到结尾都是数字
mo8 = vowelRegex.findall('123456')
print(mo8)

vowelRegex = re.compile(r'.at') # 匹配任意一个字符(除换行符)后跟at的字符串
mo9 = vowelRegex.findall('The cat in the hat sat on the flat mat.')
print(mo9)

vowelRegex = re.compile(r'First Name:(.*) Last Name:(.*)') # .* 匹配任意字符任意次
mo10 = vowelRegex.search('First Name:Aj Last Name:Sweigart.') # findall 没有group方法
print(mo10.group())
print(mo10.group(1))
print(mo10.group(2))

vowelRegex = re.compile(r'<.*?>') # .* 匹配左尖括号,中间任意字符串,右尖括号,非贪心模式
mo11 = vowelRegex.findall('<To serve man> for dinner>')
print(mo11)

vowelRegex = re.compile(r'<.*>') # .* 匹配左尖括号,中间任意字符串,右尖括号,贪心模式
mo12 = vowelRegex.findall('<To serve man> for dinner>')
print(mo12)

noNewlineRegex = re.compile('.*') # 匹配所有字符,除了换行符
mo13 = noNewlineRegex.findall('Serve the public trust.\nProtect the innocent.\nUphold the law.')
print(mo13)

noNewlineRegex = re.compile('.*',re.DOTALL) # 匹配所有字符,包括换行符
mo14 = noNewlineRegex.findall('Serve the public trust.\nProtect the innocent.\nUphold the law.')
print(mo14)

robocop = re.compile(r'robocop',re.IGNORECASE)  # 第二个参数表示不区分大小写
mo15 = robocop.findall('Robocop is part man,part machine,all cop.')
mo16 = robocop.findall('ROBOCOP protects the innocent.')
print(mo15)
print(mo16)

nameRegex = re.compile(r'Agent \w+') # sub()方法是替换,用第一个参数去替换第二个参数中符合条件的字符串
mo17 = nameRegex.sub('CENSORED','Agent Alice gave the secret documents to Agent Bob.')
print(mo17)

agentNameRegex = re.compile(r'Agent (\w)\w*')
mo18 = agentNameRegex.sub('\1****','Agent Alice told Agent Carol that Agent Eve knew Agent Bob was a double agent.')
print(mo18)

##########################################################

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值