Using regex: Regex Help Python? on newest questions tagged regex – Stack Overflow

I have this code:

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

This code is made to output everything in the parentheses after the word ‘Hello’,

Hello (Hi)  would give 'Hi'

My problem is that when I put in:

Hello('Hi')

…It still returns 'Hi' when I want it to return "'Hi'"

Does anyone know how could I fix this code?

See Answers


source: http://stackoverflow.com/questions/10661573/regex-help-python
Using regex: using-regex



online applications demo