0

I have this regex:

 ([abc])(?!\1)([abc])(?!\2|\1)([abc])

to find any permutation of "abc" (abc, acb, bac, bca, cab, and cba) in a given string such as:

 aabbccabcabbccaassbbsdddbbsbdbabc

That regex is working well, but now I need another regex to do the same but with all the permutations without repetition of "abbc" (12 combinations)

I need to find 4 characters in a string that have one "a", two "b" and one "c". No matter the order but have to be consecutive. (abbc, babc, bbca...)

My question is different than the one suggested because it has one character that has to be repeated 2 times

 1. Find [abc]
 2. Find "a" or "c" if it is not in 1.
    Find "b"
 3. Find "a" or "c" if it doesn't appear in 1. or 2.
    Find "b" if it is not alreday 2 times
 4. Find "a" or "c" if it doesn't appear in 1. or 2 or 3.
    Find "b" if it is not alreday 2 times
Kobi
  • 135,331
  • 41
  • 252
  • 292
John Mathison
  • 904
  • 1
  • 11
  • 36
  • What language are you using? – Kobi Jun 10 '15 at 07:48
  • 1
    language c#, not duplicate, here I need "1223" or "abbc" where one of the characters have to be 2 times and the others just 1 time – John Mathison Jun 10 '15 at 08:06
  • 1
    Is this regex was useful you should have marked it as answer here : http://stackoverflow.com/questions/30738714/regex-permutations-without-repetition – vks Jun 10 '15 at 08:47
  • @nhahtdh - Are you sure this a duplicate? Both other question do not ask for repeated characters, which is more complicated. Also, different languages have different solutions. – Kobi Jun 10 '15 at 10:40
  • 1
    @Kobi: The accepted solution on http://stackoverflow.com/questions/10727118/how-to-find-all-permutations-of-a-given-word-in-a-given-text can take care of such case. And it is written in pseudocode. – nhahtdh Jun 10 '15 at 10:40

1 Answers1

4

C# regex have a feature that is called balancing groups, that can be used for counting.

You can use this regex (working example at Regex Storm):

(?:(?<A>a)|(?<B>b)|(?<C>c)){4}(?<-A>)(?<-B>){2}(?<-C>)
  • (?<A>a) - Match a and push it to a stack called A.
  • (?<-A>) - Pop from the stack A.
  • (?<-B>){2} - Pop twice from the stack B.

If the regex engine cannot pop from the stack the match fails, so this gives us an easy way to count the matched charactes.

There are other approaches, including approaches that would work in other regex flavors (other languages), but this approach has a benefit that it scales well and has no repetitions: if you want 7 characters which are 4 As, 2 Bs and one C, it is a trivial change.

Community
  • 1
  • 1
Kobi
  • 135,331
  • 41
  • 252
  • 292