我想检测字符串中是否有三个相同的字母相邻
例如:
string1 = 'this is oooonly excaple' # ooo
string2 = 'nooo way that he did this' # ooo
string3 = 'I kneeeeeew it!' # eee
有什么类似Python的方法吗?
我想这样的解决方案不是最好的:
for letters in ['aaa', 'bbb', 'ccc', 'ddd', ..., 'zzz']:
if letters in string:
print(True)
Tags:
您不必使用正则表达式,但对于这样简单的事情,解决方案有点长
使用正则表达式:
输出:
您可以使用^{} 将相似的字母分组,然后检查每组的长度:
将输出:
如果您不关心字母本身,只想知道是否有重复,请利用内置的^{} 功能进行短路:
相关问题 更多 >
编程相关推荐