I\'m searching a file line by line for the occurrence of ##random_string##. It works except for the case of multiple #...
pattern=\'##(.*?)##\'
prog=re.compile(p
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: If you do not want to allow blank inner text (i.e. "####" shouldn't match with an inner text of ""), then change it to:
prog = re.compile(r'##([^#]+)##')
+
means "one or more."