Possible Duplicate:
Python Regular Expression Matching: ## ##
I already asked this question, but let me restate it better… Im searching a file line by line for the occurrence of ##random_string##. It works except for the case of multiple #…
pattern='##(.*?)##'
prog=re.compile(pattern)
string='lala ###hey## there'
result=prog.search(string)
print re.sub(result.group(1), 'FOUND', line)
Desired Output:
"lala #FOUND there"
Instead I get the following because its grabbing the whole ###hey##:
"lala FOUND there"
So how would i ignore any number of # at the beg or end, and only capture “##string##”.
Your problem is with your inner match. You use
., which matches any character that isn’t a line end, and that means it matches#as well. So when it gets###hey##, it matches(.*?)to#hey.The easy solution is to exclude the
#character from the matchable set:Protip: Use raw strings (e.g.
r'') for regular expressions so you don’t have to go crazy with backslash escapes.Trying to allow
#inside the hashes will make things much more complicated.(EDIT: Earlier version didn’t handle leading/trailing
###right.)