简体   繁体   English

排列长度为N的M个字符的正则表达式

[英]Regular expression for permutation of M characters of length N

I need a regular expression that represents permutation of given M characters and the permutation string should be in length N. For example, I have 1 and 0, so M=2 and the length of permutation string need to be 3 (ie. N=3 ), then I have: 我需要一个表示给定M个字符的排列的正则表达式,并且排列字符串的长度应为N。例如,我有1和0,所以M=2并且排列字符串的长度必须为3(即N=3 ),那么我有:

000, 001, 010, 011, 100, 101, 110, 111

When N=2 , I have: N=2 ,我有:

00, 01, 10, 11

What should the regular expression be for this? 正则表达式应该是什么呢?

Thanks! 谢谢!

Edit: Just want to make it clear, the input is not limited to digits, it can be letters, for example, I got {A, T, C, G} representing Adenine, Thymine, Cytosine and Guanine in genomics, I need to have permutation of three per group, so I can have a list like: 编辑:只是想弄清楚,输入不限于数字,它可以是字母,例如,我在基因组学中得到{A, T, C, G}代表腺嘌呤,胸腺嘧啶,胞嘧啶和鸟嘌呤,我需要每个组有三个排列,所以我可以有一个像这样的列表:

ATC, ATG, ACT, AGA etc ATC, ATG, ACT, AGA

Thanks! 谢谢!

Heres a Regex to match the digits 1-n . 这是一个与数字1-n匹配的1-n表达式。 You can tinker with it to do what you want. 您可以修改它来做您想要的。

^(?=[1-n]{n}$)(?!.*(.).*\1).*$

Sample input for n = 4 n = 4的样本输入

1234
2431
abcde
4321
1231
3412

Output 输出量

1234  *MATCH*
2431  *MATCH*
abcde
9010
4321   *MATCH*
1231   *MATCH*
3412   *MATCH*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM