mtp
mtp

Reputation: 35

Python regex to match groups of same length with match but varying between matches

i would like to match the sequence (G{x})([ACGT]{1,7})(G{x})([ACGT]{1,7})(G{x})([ACGT]{1,7})(G{x}) where x is a number between 2 and 5, which can vary between matches but must be the same between groups inside a single match. Is it possible to do this with a single regex?

Upvotes: 1

Views: 100

Answers (1)

Kobi
Kobi

Reputation: 137997

You can use backreferencing:

(G{2,5})([ACGT]{1,7})\1([ACGT]{1,7})\1([ACGT]{1,7})\1

Working example: https://regex101.com/r/yL5tE6/1

Note that it does allow more Gs than there were on the first group, because [ACGT] might add Gs in adjacent to \1.

Upvotes: 3

Related Questions