c# - Regex blocks of permutations -
thanks kobi resolve problem of checking permutations without repetition given string "abbc". solution
(?:(?<a>a)|(?<b>b)|(?<c>c)){4}(?<-a>)(?<-b>){2}(?<-c>)
the expression has have 1 "a", 2 "b" , 1 "c" in 4 consecutive characters.
now need more complicated, need check expression 2 blocks of permutations:
- in first 6 positions has find permutations of "abccc" adding each 1 character in 3rd position can of them (a, b or c).
- in second block has find permutations of "abccc"
i don't know if have explained myself well, but:
- for first 6 positions has have 1 a, 1 b, 3 c , in 3rd position value (a,b or c) no matter order except 3rd position can any
- for next 5 positions has have 1 a, 1 b , 3 c no matter order.
i'm trying explain best way possible. i'm sorry looked copy-paste not.
the regex i'm looking "abxccc-abccc" x a, b or c , - separate 2 blocks, although i'm adding - because first 6 characters have combinations , last 5 same.
it doesn't have way, similar, example. "aabxxcc-abbc" example.
it simple adapt the previous pattern new one.
the second block straightforward:
(?:(?<a>a)|(?<b>b)|(?<c>c)){5}(?<-a>)(?<-b>)(?<-c>){3}
next, thing need know can specify same group more once. explained in martin büttner's answer, i've linked before.
so, first block, [abc]
on first position, can written as:
(?:(?<a>a)|(?<b>b)|(?<c>c)){2}[abc](?:(?<a>a)|(?<b>b)|(?<c>c)){3}(?<-a>)(?<-b>)(?<-c>){3}
combined:
(?:(?<a>a)|(?<b>b)|(?<c>c)){2}[abc](?:(?<a>a)|(?<b>b)|(?<c>c)){3}(?<-a>)(?<-b>)(?<-c>){3}-(?:(?<a>a)|(?<b>b)|(?<c>c)){5}(?<-a>)(?<-b>)(?<-c>){3}
or, ignore whitespace flag:
(?:(?<a>a)|(?<b>b)|(?<c>c)){2} # match 2 a, b, or c's [abc] # match a, b, or c, don't push stack. (?:(?<a>a)|(?<b>b)|(?<c>c)){3} # match 3 a, b, or c's (?<-a>)(?<-b>)(?<-c>){3} # check we've matched 1 a, 1 b , 3 c's. - # match dash (?:(?<a>a)|(?<b>b)|(?<c>c)){5} # match 5 a, b, or c's (?<-a>)(?<-b>)(?<-c>){3} # check we've matched 1 a, 1 b , 3 c's.
this not elegant (it has repetition), should still simple understand next step of previous question.
Comments
Post a Comment