0

I've never used regular expressions before and I'm struggling to make sense of them. I have strings in the form of 'define(__arch64__)' and I just want the __arch64__.

import re  
mystring = 'define(this_symbol)||define(that_symbol)'  
pattern = 'define\(([a-zA-Z_]\w*)\)'  
re.search(mystring, pattern).groups()  

(None, None)

What doesn't search return 'this_symbol' and 'that_symbol'?

2 Answers 2

2

You have the parameters of search() in the wrong order, it should be:

re.search(pattern, mystring)

Also, backslashes are escape characters in python strings (for example "\n" will be a string containing a newline). If you want literal backslaches, like in the regular expression, you have to escape them with another backslash. Alternatively you can use raw strings that are marked by an r in front of them and don't treat backslashes as escape characters:

pattern = r'define\(([a-zA-Z_]\w*)\)'
Sign up to request clarification or add additional context in comments.

2 Comments

That fixed it. Adding the r didn't seem to make any difference though.
@Neil: Probably because \(, \) and \w aren't valid escape sequences in Python strings, so they didn't get replaced by anything else. But generally I'd recommend to use the r when dealing with strings that should contain backslashes.
0

You must differentiate between the symbol ( and the regexp group characters. Also, the pattern goes first in re.search:

pattern = 'define\\(([a-zA-Z_]\w*)\\)'
re.search(pattern, mystring).groups()  

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.