0

The regex I have right now is:

string="hello (fdd()()()()(()(())()))"
re.match("%s\s*\((.*?)\)$"%re.escape("hello"), string)

And it will work well, the goal of it is to get whatever is inside the brackets. In this case: "fdd()()()()(()(())())"

I would like to make an alteration, the regex should work with this test case

"hello (hihihi(((())))hihi) { "

There is a curly-brace at the end of it. There should always be a curly-brace at the end of the given string therefore the first test case that I showed you will not work anymore (with the new regex that I want).

Try looking at it like this

hello[any amount of space]([get WHATEVER is inside here])[any amount of space]{[any amount of space]

I think that using the dollar sign is what is causing me problems. I am obviously not too familiar with using regex, so if anyone can help me that would be great. I am open to any solution including but not limited to other python modules, built in python string features, ect.

Thanks for your help,

2
  • HINT: If you think the $ is causing you problems, have you tried removing the $ (and the ?)? Commented Jul 6, 2012 at 21:52
  • 1
    also remove ? otherwise it ends at the nearest ) Commented Jul 6, 2012 at 21:53

2 Answers 2

1

I think you could just use "hello\s*\((.+)\)\s*{"

import re

text = "hello (hihihi(((())))hihi) { "
print re.match(r'hello\s*\((.+)\)\s*{', text).group(1)

gives me

hihihi(((())))hihi

You don't need the ? and $.

Sign up to request clarification or add additional context in comments.

Comments

1

Try changing your regex to the following:

r"%s\s*\((.*?)\)\s*{" % re.escape("hello")

The only difference here is that the $ was replaced by \s*{, which means any amount of whitespace followed by a {.

Example:

>>> s = "hello (hihihi(((())))hihi) { "
>>> print re.match(r"%s\s*\((.*?)\)\s*{" % re.escape("hello"), s).group(1)
hihihi(((())))hihi

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.