我在Python中使用正则表达式从文本文件中提取数据元素。我遇到了太多括号的问题。正则表达式匹配太多圆括号
的文本存储在一个字符串名为temp和的形式为:
temp='Somethingorother School District (additional text)|other stuff here'
我目前使用
match = re.search(r'(.* School District) (\(.*\))\|?',temp)
伟大的工程和匹配
match.group(1) = Somethingorother School District
match.group(2) = (additional text)
然而,有时候'其他东西'部分也包含括号,如下所示:
'Somethingorother School District (additional text)|$59900000 (4.7 mills)'
,所以我得到
match.group(2) = (additional text)|$59900000 (4.7 mills)
我明白,这是因为*运算符是贪婪的,但(附加文本)的部分是相当的特质,我想捕捉无论是在那些括号。换句话说,我希望它在这些括号内是贪婪的,但是一旦它匹配a就停止寻找)。有没有办法做到这一点?
最好的方法是用'[^]] *'代替'。*',它会匹配任何东西,但会关闭')',所以当你遇到第一个''' – Tensibai 2014-11-21 15:09:11