如何正则表达式匹配?

时间:2014-04-01 00:20:12

标签: python regex

我有以下一行:

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&#34; re&#34;中编写匹配表达式?获得以下团体?

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

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

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

1 个答案:

答案 0 :(得分:1)

我可以看到两个问题:

  • 您在costin2010
  • 中捕获的内容超出了您的预期
  • 你没有逃过方括号

这有效:

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