c# - Parameter regex -
i need creating regex validating parameter string.
parameter string consists of 2 optional groups of explicit characters. first group can contain 1 occurrence of p, o, z characters (order doesn't matter). second group has same restrictions can contain characters t, c, p, m. if both groups presented, need delimited single space character.
so valid strings are:
p t po t ozp ct p tcmp p pz t tp
etc.
i don't think regex solution here, because have quite complicated:
regex regexobj = new regex( @"^ # start of string (?: # start non-capturing group: ([poz]) # match , capture 1 of [poz] in group 1 (?![poz]*\1) # assert that character doesn't show again )* # repeat number of times (including zero) (?: # start non-capturing group: (?<!^) # assert we're not @ start of string \ # match space (?!$) # assert we're not @ end of string )? # make group optional. (?<! # assert we're not right after... [poz] # 1 of [poz] (i. e. make sure there's space) (?!$) # unless we're @ end of string. ) # end of negative lookahead assertion (?: # start yet non-capturing group: ([tcpm]) # match , capture 1 of [tcpm] in group 2 (?![tcpm]*\2) # assert that character doesn't show again )* # repeat number of times (including zero) $ # end of string", regexoptions.ignorepatternwhitespace);
Comments
Post a Comment