Python 使用最后出现在原始文本中的模式格式搜索specfic子字符串

Python 使用最后出现在原始文本中的模式格式搜索specfic子字符串,python,python-3.x,regex,Python,Python 3.x,Regex,我有一个字符串,比如: Location: FD130 New York GA Unit Serial Purchase Order Sales Order Make Model Amount Tax Total 12345566 Location: FD130 Roswell GA Unit Serial Purchase Order Sales Order Make Model Amount Tax Total 0130029964 当我尝试正则表达式时,我希望获得指定序列号(34573

我有一个字符串,比如:

Location: FD130 New York GA Unit Serial Purchase Order Sales Order Make Model Amount Tax Total 12345566 Location: FD130 Roswell GA Unit Serial Purchase Order Sales Order Make Model Amount Tax Total 0130029964 
当我尝试正则表达式时,我希望获得指定序列号(34573)的位置。 正则表达式:
'位置:(.*)单位(.*)0130029964

然后给出整个字符串的位置,并以34573结尾

当我通过
0130029964
序列id时,预期输出为
FD130纽约GA


python中是否有函数用于从给定序列号中获取第一个向后的子字符串
location

您可以使用一种不交叉匹配
location:
unit
format
的方法来匹配第一次出现的
unit

然后可以选择匹配
格式
并匹配到特定数字,而不交叉匹配
位置:

\blocation:\s*((?:(?!location:|unit|format).)+) (?:format )?unit (?:(?!location:).)+\b12345566\b

第一组按原样给出整个字符串。
r'位置:\s*(\s+[^.?!]*单元[^.?!]*\b34573\b'
?看,实际上我尝试了这个正则表达式,它给出了整个字符串,从位置开始,以34573 id结束。我被告知正则表达式在这里不起作用。看。使用
m=re.search(rx,text,re.I)
,您将获得
drish
。也许您也可以使用
r“位置:\s*([^.?!]*?)\s*格式\s+单位[^.?!]*\b34573\b”
,请参阅Try
\b位置:\s*(?:(!位置:|单位|格式)。+(?:格式)?单位(?(!位置:)+\b0130029964\b
请参阅和