Python 正则表达式匹配太多的括号集
我正在使用python中的正则表达式从文本文件中提取数据元素。我遇到了抓取太多括号的问题 文本存储在名为temp的字符串中,其格式为:Python 正则表达式匹配太多的括号集,python,regex,Python,Regex,我正在使用python中的正则表达式从文本文件中提取数据元素。我遇到了抓取太多括号的问题 文本存储在名为temp的字符串中,其格式为: temp='Somethingorother School District (additional text)|other stuff here' 我目前正在使用 match = re.search(r'(.* School District) (\(.*\))\|?',temp) 它很好用,而且很相配 match.group(1) = Something
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)匹配,就停止查找。有什么方法可以做到这一点吗?使用
[^()]*
匹配任何字符,但不匹配(
或)
零次或多次
将非贪婪放在最后一个括号中。最好的方法是将
*
替换为[^]*
,它将匹配除结束)
之外的任何内容,因此当您遇到第一个时,它将停止匹配。
>>> match = re.search(r'(.* School District) (\([^()]*\))\|?',temp)
>>> match.group(1)
'Somethingorother School District'
>>> match.group(2)
'(additional text)'