re.DOTALL
您可以通过启用两个捕获组和选项来执行此操作。该表达式假定您感兴趣的号码是文本中唯一一个有 10 位数字的号码。
表达式为:
(PO\sNumber).*(\d{10})
Python 片段:
import re
first_string = """PO Number Dept.number
4000813852 7"""
second_string = """Invoice Number PO Number Dept.number
123456666 4000813853 7"""
PO_first = re.search(r'(PO\sNumber).*(\d{10})',first_string,re.DOTALL)
print(PO_first.group(1)+" "+PO_first.group(2))
PO_second = re.search(r'(PO\sNumber).*(\d{10})',second_string,re.DOTALL)
print(PO_second.group(1)+" "+PO_second.group(2))
输出:
PO Number 4000813852
PO Number 4000813853