如何从以下python字符串中提取序列号:
blah: asdf
blah: asdf
Serial Number: 1234ABCD
blah asdf
blah asdf
我尝试过以下操作,但似乎无法正常工作:
serial_num = re.search("^Serial Number:\s*(\w*)$", serial_num, re.MULTILINE)
答案 0 :(得分:2)
您可以使用以下正则表达式 ...
Serial\sNumber.*?(?=\w)(\w+)
参见 regex demo
python (demo)
import re
s = """
blah: asdf
blah: asdf
Serial Number: 1234ABCD
blah asdf
blah asdf
"""
r = r"Serial\sNumber.*?(?=\w)(\w+)"
m = re.findall(r, s)
print(m)[0]