[英]Python Regex to Extract Word From Multiline String
How would I extract the serial number from the following python string: 我如何从以下python字符串中提取序列号:
blah: asdf
blah: asdf
Serial Number: 1234ABCD
blah asdf
blah asdf
I have tried the following, but it doesn't appear to be working: 我已经尝试了以下方法,但似乎无法正常工作:
serial_num = re.search("^Serial Number:\s*(\w*)$", serial_num, re.MULTILINE)
You may perhaps use the following regex ... 您也许可以使用以下正则表达式 ...
Serial\sNumber.*?(?=\w)(\w+)
see regex 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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.