使用原始文本中最后一次出现的模式格式搜索特定子字符串



我有一个字符串,比如:

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 

当我尝试regex时,喜欢获取指定序列号(34573(的位置。Regex:'Location:(.*)?Unit(.*?)0130029964

然后它给出了整个字符串的位置,并以34573结束。

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

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

您可以使用调和贪婪令牌方法在unit的第一次出现时匹配位置单元,以不交叉匹配location:unitformat

然后可选地匹配format并匹配到特定数字,而不必再次交叉匹配location:

blocation:s*((?:(?!location:|unit|format).)+) (?:format )?unit (?:(?!location:).)+b12345566b

Regex演示

最新更新