“”""
match()传入要匹配的字符串以及正则表达式,就可以检测这个正则表达式是否匹配字符串。
match()方法会尝试从字符串的起始位置匹配正则表达式,
如果匹配,就返回匹配成功的结果,
如果不匹配,就返回None。
“”"
import re
a = “Book:ps125689589 Corporation:sayhello”
result = re.match(“Book:\w{8}”,a)
print(result.group())
运行结果:Book:ps125689
result1 = re.match(“Book.\w",a)
result2 = re.match(".\w”,a)
print(result1.group(),result2.group())
运行结果:Book:ps125689589 Corporation:sayhello Book:ps125689589 Corporation:sayhello
result3 = re.match("\w{4}:\w\w(\d+)",a)
print(result3.group(1))
运行结果:125689589
result4 = re.match("B.(\d+)",a) # 万能匹配符 贪婪法则
result5 = re.match(“B.*?(\d+)”,a) # 非贪婪法则
print(result4.group(1))
print(result5.group(1))