如何使用regexp为mysql匹配字母的偶数或字母的奇数 [英] How do you match even numbers of letter or odd numbers of letter using regexp for mysql

查看:166
本文介绍了如何使用regexp为mysql匹配字母的偶数或字母的奇数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何在mysql中使用regexp匹配字母的偶数和奇数吗?我需要匹配偶数个A,然后匹配个奇数个G,然后至少匹配一个TC?例如:acgtccAAAAGGGTCatg将匹配.这是dna测序

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

推荐答案

偶数个A可以表示为(AA)+(AA的一个或多个实例;因此它将匹配AA,AAAA,AAAAAA. ..).奇数个G可以表示为G(GG)*(一个G后跟零个或多个GG实例,以便匹配G,GGG,GGGGG ...).

An even number of A's can be expressed as (AA)+ (one or more instance of AA; so it'll match AA, AAAA, AAAAAA...). An odd number of Gs can be expressed as G(GG)* (one G followed by zero or more instances of GG, so that'll match G, GGG, GGGGG...).

将它们放在一起,您将得到:

Put that together and you've got:

/(AA)+G(GG)*TC/

但是,由于正则表达式引擎将尝试尽可能地匹配,因此该表达式实际上将匹配AAAGGGTC的子字符串(即AAGGGTC)!为了防止这种情况,您可以使用负向后看来确保该字符位于第一个A不是另一个A:

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 first A isn't another A:

/(?<!A)(AA)+G(GG)*TC/

...除了MySQL在其正则表达式中不支持环视.

...except that MySQL doesn't support lookarounds in their regexes.

您可以做的是指定模式在字符串的开头( 之前按^),或者前面的字符不是A:

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:

/(^|[^A])(AA)+G(GG)*TC/

但是请注意,使用此模式时,如果在字符串开头找不到该模式,则会捕获一个额外的字符,因此,如果不是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.

这篇关于如何使用regexp为mysql匹配字母的偶数或字母的奇数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆