一、查找模式
import re
pattern = 'this'
text = 'Does this text match the pattern?'
match = re.search(pattern, text)
s = match.start()
e = match.end()
print('Found "{}"\nin "{}"\nfrom {} to {} ("{}")'.format(
match.re.pattern, match.string, s, e, text[s:e]))
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: {!r}\n'.format(text))
for regex in regexes:
print('Seeking "{}" ->'.format(regex.pattern), end=' ')#end是個格式化輸出,預設是換行,這處不換行
if regex.search(text):
print('match!')
else:
print('no match')
# Text: 'Does this text match the pattern?'
#
# Seeking "this" -> match!
# Seeking "that" -> no match
三、簡單示例
3.1、修改字元串
import re
bold = re.compile(r'\*{2}(.*?)\*{2}')
text = 'Make this **bold**. This **too**.'
print('Text:', text)
print('Bold:', bold.sub(r'<b>\1</b>', text))
# Bold: Make this <b>bold</b>. This <b>too</b>.
3.2、拆分字元串
import re
text = '''Paragraph one
on two lines.
Paragraph two.
Paragraph three.'''
for num, para in enumerate(re.findall(r'(.+?)\n{2,}',
text,
flags=re.DOTALL)
):
print(num, repr(para))
print()
# 0 'Paragraph one\non two lines.'
#
# 1 'Paragraph two.'