简体   繁体   中英

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:

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

When N=2 , I have:

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:

ATC, ATG, ACT, AGA etc

Thanks!

Heres a Regex to match the digits 1-n . You can tinker with it to do what you want.

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

Sample input for n = 4

1234
2431
abcde
4321
1231
3412

Output

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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