Python 如何进行正则表达式匹配?

Python 如何进行正则表达式匹配?,python,regex,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) 我尝试了以下操作,但无效: (?P<line>.+?)\)

我有以下几行:

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)
我尝试了以下操作,但无效:

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

我可以看到两个问题:

  • 在2010年的
    成本中,您捕获了比您应该捕获的更多
  • 你没有逃过方括号
这项工作:

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