import re
pattern = 'this'
text = 'Does this text match the pattern?'
match = re.search(pattern, text)
s = match.start()
e = match.end()
print('Found "{0}"\nin "{1}"'.format(match.re.pattern, match.string))
print('from {0} to {1} ("{2}")'.format( s, e, text[s:e]))
#python re_simple_match.py
Found "this"
in "Does this text match the pattern?"
from 5 to 9 ("this")
import re
# Precompile the patterns
regexes = [ re.compile(p) for p in ('this', 'that')]
text = 'Does this text match the pattern?'
print('Text: {0}\n'.format(text))
for regex in regexes:
if regex.search(text):
result = 'match!'
else:
result = 'no match!'
print('Seeking "{0}" -> {1}'.format(regex.pattern, result))
#python re_simple_compiled.py
Text: Does this text match the pattern?
Seeking "this" -> match!
Seeking "that" -> no match!
import re
text = 'abbaaabbbbaaaaa'
pattern = 'ab'
for match in re.findall(pattern, text):
print('Found "{0}"'.format(match))
#python re_findall.py
Found "ab"
Found "ab"
import re
text = 'abbaaabbbbaaaaa'
pattern = 'ab'
for match in re.finditer(pattern, text):
s = match.start()
e = match.end()
print('Found "{0}" at {1}:{2}'.format(text[s:e], s, e))
#python re_finditer.py Found "ab" at 0:2 Found "ab" at 5:7
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有