import re
nameRegex = re.compile(r'First Name:(.*) Last Name:(.*)')
mo = nameRegex.search('First Name:Al Last Name:Sweigart')
print(mo.group(1))
print(mo.group(2))
# 非贪心模式匹配所有文本
nong = re.compile(r'<.*?>')
mo1 = nong.search('<to server man> for dinner.>')
print(mo1.group())
# 贪心模式匹配所有文本
greedy = re.compile(r'<.*>')
mo2 = greedy.search('<to server man> for dinner.>')
print(mo2.group())
结果是:
Al
Sweigart
<to server man>
<to server man> for dinner.>