Does anyone know how to match even numbers and odd numbers of letter using regexp in mysql? i need to match like a even number of A’s followed by an odd number of G’s and then at least one TC? For example: acgtccAAAAGGGTCatg would match up. It’s something for dna sequencing
Share
An even number of A’s can be expressed as
(AA)+(one or more instance ofAA; so it’ll match AA, AAAA, AAAAAA…). An odd number of Gs can be expressed asG(GG)*(oneGfollowed by zero or more instances ofGG, so that’ll match G, GGG, GGGGG…).Put that together and you’ve got:
However, since regex engines will try to match as much as possible, this expression will actually match a substring of
AAAGGGTC(ie.AAGGGTC)! In order to prevent that, you could use a negative lookbehind to ensure that the character before the firstAisn’t anotherA:…except that MySQL doesn’t support lookarounds in their regexes.
What you can do instead is specify that the pattern either starts at the beginning of the string (anchored by
^), or is preceded by a character that’s not A:But note that with this pattern an extra character will be captured if the pattern isn’t found at the start of the string so you’ll have to chop of the first character if it’s not an A.