views:

40

answers:

3

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##".

A: 
>>> s='lala ###hey## there'
>>> re.sub("(##[^#]+?)#+","FOUND",s)
'lala #FOUND there'

>>> s='lala ###hey## there blah ###### hey there again ##'
>>> re.sub("(##[^#]+?)#+","FOUND",s)
'lala #FOUND there blah ####FOUND'
ghostdog74
+1  A: 

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:

prog = re.compile(r'##([^#]*)##')

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.)

Mike DeSimone
thanks, but it doesnt work for the string ####hey## =( lol
nubme
Moved my answer to your original question, look there.
Mike DeSimone
A: 
import re

pattern = "(##([^#]*)##)"
prog = re.compile(pattern)

str = "lala ###hey## there"
result = prog.search(string)

line = "lala ###hey## there"

print re.sub(result.group(0), "FOUND", line)

The trick is to say (not #) instead of anything. This also assumes that

line = "lala #### there"

results in:

line = "lala FOUND there"
Chris McMillan
You don't need to put parens around the whole pattern; match group 0 is the whole matched input text.
Mike DeSimone