[英]How to find maximum consecutive occurrences of a substring in a string?
I want a function that will take a full string and a sub-string and returns the maximum consecutive occurrences of that sub-string.我想要一个 function 它将采用一个完整的字符串和一个子字符串并返回该子字符串的最大连续出现次数。 I know I have to use regular expressions but I am not very familiar with the syntax and I'm a little stuck.我知道我必须使用正则表达式,但我对语法不是很熟悉,而且我有点卡住了。
So it should work something like this:所以它应该像这样工作:
import re
def get_max(substring, fullstring):
???? #TO-DO
return max
print(get_max(foo, foofoofooxxfoofoo)
Output: 3
Using re
:使用re
:
import re
def get_max(substring, full_string):
pattern = "(?=((" + re.escape(substring) + ")+))"
matches = re.findall( pattern, full_string )
return max(len(m[0]) // len(substring) for m in matches
print(get_max('foo', 'foofoofooxxfoofoo'))
OUTPUT: OUTPUT:
3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.