4

I'm trying to construct a regular expression that will match a repeating DNA sequence of 2 characters. These characters can be the same.

The regex should match a repeating sequence of 2 characters at least 3 times and, here are some examples:

regex should match on:

  • ATATAT
  • GAGAGAGA
  • CCCCCC

and should not match on:

  • ACAC
  • ACGTACGT

So far I've come up with the following regular expressions:

[ACGT]{2}

this captures any sequence consisting of exactly two characters (A, C, G or T). Now I want to repeat this pattern at least three times, so I tried the following regular expressions:

[ACGT]{2}{3,}
([ACGT]{2}){3,}

Unfortunately, the first one raises a 'multiple repeat' error (Python), while the second one will simply match any sequence with 6 characters consisting of A, C, G and T.

Is there anyone that can help me out with this regular expression? Thanks in advance.

2 Answers 2

8

You could perhaps make use of backreferences.

([ATGC]{2})\1{2,}

\1 is the backreference referring to the first capture group and will be what you have captured.

regex101 demo

Sign up to request clarification or add additional context in comments.

6 Comments

Will match ACAC (ACACACAC certainly) - not downvoting because I have no idea what the OP wants
@AlecTeal Entire expression will not match match ACAC. :)
@thefourtheye so the OP wants to reject only ACAC? This sounds like a job for simple if statements in that case, the comment stays because I am not convinced it does not apply.
@AlecTeal He wants to match any two letter combination of AGCT and that should be repeated atleast twice.
Cases like AAATTTAAATTTAAA aren't caught by this pattern.
|
0

One:

(AT){3}

Two

(GA){4}

Three

C{6}

Combining them!

(C{6}|(GA){4}|(AT){3})

11 Comments

He wants to match something like ACACAC or any combination of letters in AGCT
@thefourtheye I don't really follow
He wants to match patterns like, ACACAC, ACACACAC, GCGCGC
@thefourtheye then why is RedNaw's answer not the accepted one?
Because he deleted it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.