2014-11-21 69 views
1

我在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就停止尋找)。有沒有辦法做到這一點?

+1

最好的方法是用'[^]] *'代替'。*',它會匹配任何東西,但會關閉')',所以當你遇到第一個''' – Tensibai 2014-11-21 15:09:11

回答

2

使用negated character class

>>> match = re.search(r'(.* School District) (\([^()]*\))\|?',temp) 
>>> match.group(1) 
'Somethingorother School District' 
>>> match.group(2) 
'(additional text)' 

[^()]*任何字符,但不()零次或多次匹配。

DEMO

+0

Demo [here]時會停止匹配。 http://regex101.com/r/pO2sU3/1)(以防萬一它可能幫助它) – Tensibai 2014-11-21 15:11:43

+0

感謝您的演示鏈接。 – 2014-11-21 15:13:38

1

將非貪婪的最後一個括號。