加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
2016.02.26 re的五个测试 2.42 KB
一键复制 编辑 原始数据 按行查看 历史
现任明教教主-乾颐堂 提交于 2016-02-26 10:52 . new file
import re
result = re.split('---', 'aaa---bbb---ccc')
print(result)
result = re.sub('--', '...', 'aaa--bbb--ccc')
print(result)
result = re.split('[-=]', 'aaa-bbb=ccc')
print(result)
result = re.split('(--)|(==)', 'aaa--bbb==ccc')
print(result)
result = re.split('(?:--)|(?:==)', 'aaa--bbb==ccc')
print(result)
=========================================================
import re
str1 = 'spam/ham/eggs'
result = str1.split('/')
print(result)
result = re.match('(.*)/(.*)/(.*)', str1).groups()
print(result)
str2 = '<spam>/<ham>/<eggs>'
result = re.match('<(.*)>/<(.*)>/<(.*)>', str2).groups()
print(result)
str3 = ' <spam>/<ham><eggs>'
result = re.match('\s*<(.*)>/?<(.*)>/?<(.*)>', str2).groups()
print(result)
str4 = 'Hello pattern world!'
result = str4.split()
print(result)
str5 = 'Hellopattern world !'
result = re.match('Hello\s*([a-z]*)\s+(.*?)\s*!', str5).groups()
print(result)
====================================================================
import re
str1 = '<spam>/<ham>/<eggs>'
result = str1.find('ham')
print(result)
result = re.findall('<(.*?)>', str1)
print(result)
str2 = '<spam> / <ham>/<eggs>'
result = re.findall('<(.*?)>', str2)
print(result)
str3 = '<spam>/<ham> ... <eggs>/<cheese>'
result = re.findall('<(.*?)>/?<(.*?)>', str3)
print(result)
str4 = 'todays menu: <spam>/<ham> ... <eggs><s>'
result = re.search('<(.*?)>/?<(.*?)>', str4).groups()
print(result)
========================================================
import re
str1 = '<spam> \n <ham>\n<eggs>'
result = re.findall('<(.*?)>.*<(.*?)>', str1)
print(result)
#[]
result = re.findall('(?s)<(.*?)>.*<(.*?)>', str1)
print(result)
#[('spam', 'eggs')]
result = re.findall('(?s)<(.*?)>.*?<(.*?)>', str1)
print(result)
#[('spam', 'ham')]
========================================================
import re
str1 = '...aaa/bbb/ccc]'
result = re.search('(?P<part1>\w*)/(?P<part2>\w*)', str1).groups()
print(result)
#('aaa', 'bbb')
result = re.search('(?P<part1>\w*)/(?P<part2>\w*)', str1).groupdict()
print(result)
#{'part1': 'aaa', 'part2': 'bbb'}
result = re.search('(?P<part1>\w*)/(?P<part2>\w*)', str1).group(2)
print(result)
#bbb
result = re.search('(?P<part1>\w*)/(?P<part2>\w*)', str1).group('part2')
print(result)
#bbb
str2 = '...aaa/bbb ccc/ddd]'
result = re.findall('(?P<part1>\w*)/(?P<part2>\w*)', str2)
print(result)
#[('aaa', 'bbb'), ('ccc', 'ddd')]
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化