Skip to main content
 首页 » 编程设计

python之如何正则表达式匹配

2025年05月04日23lonelyxmas

我有以下行:

mystr = "1A) Monkey Hoop Jumping (MHJ) and Ring Toss (.50 Cents [YEAR2010]; .25 Cents [YEAR2016]) - The performance is estimated to be cheaper (MHJ;)" 
mygroups = re.match('<match expression>', mystr) 

如何在 python "re"中编写匹配表达式来获取以下组?

line = "1A" 
activity = "Monkey Hoop Jumping (MHJ) and Ring Toss" 
costin2010 = ".50" 

我尝试了以下但它不起作用:

(?P<line>.+?)\) (?P<activity>.*?) \((?P<costin2010>[\d.]+ Cents [YEAR2010].*?) 

请您参考如下方法:

我看到的两个问题:

  • 您在 costin2010
  • 中捕获的内容超出了应有的范围
  • 你没有跳出方括号

这个有效:

(?P<line>.+?)\) (?P<activity>.*?) \((?P<costin2010>[\d.]+) Cents \[YEAR2010]