2

我有这个代码:

import re
#TEST CASES
match_dict = ['hello(here)',
             'Hello   (Hi)',
             "'dfsfds Hello (Hi) fdfd' Hello (Yes)",
             "Hello ('hi)xx')",
             "Hello  ('Hi')"]
for s in match_dict:
    print "INPUT: %s" % s
    m = re.sub(r"(?<!\()'[^']+'", '', s, flags=re.M)
    paren_quotes = re.findall(r"Hello\s*\('([^']+)'\)", m, flags=re.M)
    output = paren_quotes if paren_quotes else []
    m = re.sub(r"Hello\s*\('[^']+'\)", '', m, flags=re.M)
    paren_matches = re.findall(r"Hello\s*\(([^)]+)\)", m, flags=re.M)
    if paren_matches:
        output.extend(paren_matches)
    print 'OUTPUT: %s\n' % output

此代码用于输出单词“Hello”之后括号中的所有内容,

Hello (Hi)  would give 'Hi'

我的问题是当我输入时:

Hello('Hi')    

...'Hi' 当我想要它返回时它仍然返回"'Hi'"

有谁知道我该如何修复此代码?

4

2 回答 2

5

只需使用非贪婪匹配:

matches = re.search(r'^Hello\s*\((.*?)\)', text)
于 2012-05-19T01:56:14.907 回答
2
>>> import re
>>> p = re.compile(r'Hello\s*\((.*?)\)', re.M)
>>> m = p.findall("Hello  ('Hi')")
>>> print m
["'Hi'"]
>>> m = p.findall("'dfsfds Hello (Hi) fdfd' Hello (Yes)")
>>> print m
['Hi', 'Yes']
于 2012-05-19T02:03:47.943 回答