Regex to find a substring with a specific length that contains minimum occurrences of a specific characters -
is there regex find substring specific length contains minimum number of specific char occurrences?
for example have string such as: aababaaaaa
string have substring length 5 contains 2 b => aabab
regex should find it.
but aaaabaaaab
there not substring length of 5 contains 2 b.
suppose our string contains , b , want find substring length of 5 contains @ least 2 b:
aaaabaaaab -> invalid aaaaaaaabb -> valid aaaaaaaaaabaabaaaaaa -> valid aaaabaaaaaaabaaaaaaa -> invalid
brute force:
.b..b|b...b|..bb.|.b.b.|..b.b|bb...|b.b..|...bb|b..b.|.bb..
well, know such regular expression not parametrizable. on other hand it's possible obtain programmatically (the example in python):
import itertools def get_regex(char, charnum, strsize): chars = char * charnum + "." * (strsize - charnum) return "|".join("".join(x) x in set(itertools.permutations(chars))) print get_regex("b", 2, 5)
Comments
Post a Comment