String count with overlapping occurrences

前端 未结 22 3077
耶瑟儿~
耶瑟儿~ 2020-11-21 23:25

What\'s the best way to count the number of occurrences of a given string, including overlap in Python? This is one way:

def function(string, str_to_search_f         


        
22条回答
  •  囚心锁ツ
    2020-11-21 23:45

    That can be solved using regex.

    import re
    def function(string, sub_string):
        match = re.findall('(?='+sub_string+')',string)
        return len(match)
    

提交回复
热议问题