Python Regular Expression Matching: ## ##

前端 未结 7 862
后悔当初
后悔当初 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-26 00:01

    Adding + to regex, which means to match one or more character.

    pattern='#+(.*?)#+'
    prog=re.compile(pattern)
    
    string='###HEY##'
    result=prog.search(string)
    print result.group(1)
    

    Output:

    HEY
    
    0 讨论(0)
提交回复
热议问题