首页 > 解决方案 > Python re模块,单词出现的长度

问题描述

我有正则表达式

m=re.search('z+',"i want to zzz zzzzzzz this is end")

如何找到忽略空格10(3z + 7z)的连续z的长度

"example str want to zz z this is end" ->3(2+1)

我是新来的,如有遗漏请评论

标签: pythonregexre

解决方案


您将需要使用稍微不同的正则表达式,例如z[z ]*返回一串 z 和空格,然后用于s.count()计算有多少 z:

matches = re.findall("z[z ]*", "i want to zzz zzzzzz this is end")
result = [s.count('z') for s in matches]
print( result )

输出:

[9]

推荐阅读