Python Regular Expression Matching: ## ##

前端 未结 7 866
后悔当初
后悔当初 2021-01-25 23:31

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         


        
7条回答
  •  清歌不尽
    2021-01-25 23:44

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

提交回复
热议问题