我试过下面这样的代码。在
re.findall(r'(\d{2}){2}', 'shs111111111')
我想得到的结果是
^{pr2}$
但结果是
['11', '11']
编辑:
我在这个例子中犯了一些错误,我真正需要的是找到所有重复的子串。在
像这样:
re.findall(r'([actg]{2,}){2,}', 'aaaaaaaccccctttttttttt')
我喜欢的结果是['aaaaaaa','ccccccc','tttttttt']
但我得到了['aa'、'cc'、'tt']
有什么问题,我该怎么做?在
Tags:
re.findall
返回所有组。所以使用只需使组
non capturing
。在相关文件摘录:
^{pr2}$用这个和
我相信你需要这个正则表达式:
编辑:根据编辑的问题,您可以使用:
^{pr2}$从每对中抓取一组。在
使用
finditer
:您无法获得纯}等,然后使用
['aaaaaaa', 'ccccc', 'tttttttttt']
,因为您需要一个捕获组来使用back引用检查重复性。这里,您有一个名为groupletter
的regex,它将包含a
,或{(?P=letter)+)
反向引用来匹配所有的组重复。在((?P<letter>[a-zA-Z])(?P=letter)+)
您只能将此正则表达式与@anubhava的帖子中描述的
finditer
一起使用。在相关问题 更多 >
编程相关推荐