避免在正则表达式中捕获不需要的可选单词

时间:2017-01-17 15:24:23

标签: python regex

我有以下示例文字

text = '. Double-Decker advances to 1st on throwing error. Chris Parmelee to 3rd.'

我想捕获以下信息

['Double-Decker',  '1st']
['Chris Parmelee', '3rd']

我正在尝试以下正则表达式

'\. ([A-Za-z\'\-\s]*) (?:advances)*to (1st|2nd|3rd)'

但它匹配

['Double-Decker advances', '1st']
['Chris Parmelee'        , '3rd']

我应该如何处理不需要的可选字advances

1 个答案:

答案 0 :(得分:2)

使用:

A2ZV50J4W1RKNI is called "Non-Amazon", using USD and english (en_US), might be the american sandbox
A1MQXOICRS2Z7M is called "SI CA Prod Marketplace", using CAD and english (en_CA), might be the canadian sandbox

在你的正则表达式中有两个小错误:

  1. \. ([A-Za-z\'\-\s]*?) (?:advances )*to (1st|2nd|3rd) 运营商贪婪:您应该使用*
  2. [A-Za-z\'\-\s]*?之后没有空格,所以它不会匹配“进展”。