python - 如何在正则表达式中获得重叠的文本匹配

标签 python regex overlapping-matches

我正在使用以下内容来获取所有匹配项,包括根据 recommendations 重叠在 other线程:

[(m.start(0), m.end(0)) for m in re.findall(t,s,overlapped = True)]

其中 t 是 s 的子集。但是,我收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: findall() got an unexpected keyword argument 'overlapped.'

我做错了什么/重叠了一个过时的标志/你会怎么做?非常感谢所有帮助。

最佳答案

正如 Cunningham 和 Klaus 所提到的,我所指的标志来自另一个非 re 的包。

不过,我想出了一个无需下载外部包的解决方案,使用前瞻:

[(m.start(0), m.end(0)) for m in re.finditer('(?='+t+')',s)]

当 s = 'GATATATGCATATACTT' 且 t = 'ATAT' 时,您会得到 [(1, 1), (3, 3), (9, 9)]。我不需要返回匹配中的文本,只需要返回索引,所以它是否匹配 ['','',''] 并不重要。

关于python - 如何在正则表达式中获得重叠的文本匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31659576/

相关文章:

mysql - 将重叠的日期拆分为不同的记录

python - 匹配并索引所有子字符串,包括重叠的子字符串

java - 正则表达式拆分成重叠的字符串

python - L-BFGS-B 中的拉格朗日乘子

python - libtorrent dht 对等请求?

regex - 如何用正则表达式匹配第一个单词?

regex - 如何在 Grep 中使用反向引用

php - 使用 preg_match_all 匹配模式并排除子字符串

python - 将 float 转换为 int 并保留空值

python - 为什么hasattr会执行@property装饰器代码块