簡體   English   中英

使用在原始文本中最后出現的模式格式搜索特定的 substring

[英]Search the specfic substring with a pattern format having last occurrence within a raw text

我有一個像這樣的字符串:

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:(.*)?Unit(.*?)0130029964

然后它為整個字符串提供位置並以 34573 結尾。

當我通過0130029964序列號時,預期的 output 是FD130 New York GA

python 中是否有任何 function 用於從給定的序列號獲取第一個后向子字符串location

您可以使用緩和的貪婪令牌方法匹配位置單元第一次出現的unit ,以不交叉匹配任何location: unitformat

然后可選地匹配format並匹配到特定的數字而不跨越匹配location:再次。

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

正則表達式演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM