正则表达式的所有排列

时间:2017-08-22 18:23:07

标签: regex c++11

我想构造一个正则表达式,它将匹配所有字符串,这些字符串至少出现 M,N,P,Q 的每个字符,并且可能无限出现 A ,C,G,T (一些例子是MNPQA,MNAAPQ,MNPQ,MAPGNQC等)。我发现的一种方法是 MNPQ [ACGT] * 并得到结果的所有排列。问题是我们是否可以构造一个只生成我想要的所有字符串的正则表达式,或者如果不可能,我还可以使用C ++ 11 regexp API检查字符串中所有置换的正则表达式(第一个选项)的出现吗?

1 个答案:

答案 0 :(得分:0)

在这种情况下使用正则表达式可能不是最好的选择。它超级凌乱,运行缓慢,但是,你走了。 \b[ACGT]*([MNPQ])[ACGTMNPQ]*((?!\1)[MNPQ])[ACGTMNPQ]*((?!\1|\2)[MNPQ])[ACGTMNPQ]*((?!\1|\2|\3)[MNPQ])[ACGTMNPQ]*\b

此处显示的示例:https://regex101.com/r/9iwbC7/5

\b                    # Start of word
[ACGT]*               # Possible ACGT chars
([MNPQ])              # M,N,P or Q 
[ACGTMNPQ]*           # Filler characters
((?!\1)               # Lookahead ensures not in previous match.
[MNPQ])               # M,N,P or Q
[ACGTMNPQ]*           # Filler characters
((?!\1|\2)            # Lookahead ensures not in previous matches.
[MNPQ])               # M,N,P or Q
[ACGT]*               # Filler characters
((?!\1|\2|\3)[MNPQ])  # M,N,P, or Q but not one that happened before
[ACGTMNPQ]*           # Filler characters
\b                    # End of Word