我在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