2

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?

5
  • Please use an appropriate title which shortly describes your problem. We know that you need help, otherwise you would not ask. We also know that your question is about regular expressions and Python because it is tagged as such. Commented May 19, 2012 at 1:48
  • So... You're trying to capture the text between parentheses? Commented May 19, 2012 at 1:52
  • 2
    Am I wrong, or does this seem like an overcomplicated approach to the goal? Commented May 19, 2012 at 1:56
  • Yeah but theres a few rules I have created, Commented May 19, 2012 at 1:56
  • 1
    gskinner.com/RegExr is a great tool to mess around with RegEx Commented May 19, 2012 at 13:51

2 Answers 2

5

Just use non-greedy matching:

matches = re.search(r'^Hello\s*\((.*?)\)', text)
Sign up to request clarification or add additional context in comments.

11 Comments

This test case shouldn't work: "'Hello (f) '" , there should be nothing returned because the Hello statement is in quotes
Why does it need to be so complicated?
@user1357159: I think that no matter what you suggest about the rules, Blender is just going to modify the regex to suit in less code than you were attempting.
You know the rules and so do I, ;;Ok, enough of that, this works pretty well.
@user1357159: you are doubling up * and +. Just replace it. I made an edit for Blender
|
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']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.