如何匹配正则表达式?

2024-05-15 22:24:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下几行:

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].*?)

Tags: andrematchlineactivitymonkeyringmystr
1条回答
网友
1楼 · 发布于 2024-05-15 22:24:54

我可以看到两个问题:

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

这样做有效:

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

相关问题 更多 >