regex - C# Regular Expression pattern is not working -


here pattern. doesn't seems working. looking colorcode 2 3 alphanumeric. doesn't seems working.

^(?<productno>\d{6})\s(?<mattype>\w+).*?(\s)?((?<colorcode>[a-z0-9]{3})|(?<viewcode>\(view\d+\))*)(?:_[a-z])?$ 

with result:

123456 stli ocean ehc_q  colorcode: ehc valid 123456 stli ocean ehcd_q  colorcode: hcd invalid 

can me how make work colorcode should 2 3 alphanumeric?

valid test datas:

123456 stli_q 123456 stli ocean ehc_q 123456 stli white we_q 123456 fmv spin fuchsia 2yp 123456 stli (view002)_q 123456 stl aqua wbp 

thanks

i think can use

^(?<productno>\d{6})\s(?<mattype>\w+).*?\s*(?:(?<viewcode>\(view\d+\))|(?<colorcode>\b[a-z0-9]{2,3}))?(?:_[a-z])?$ 

see regex demo

the main change added word boundary \b @ start of colorcode pattern, , changed limiting quantifier {2,3} match 2 or 3 uppercase ascii letters or digits.

i removed capturing groups ((\s)? > \s*, may use \s? if there can 1 or 0 whitespaces) , turned "container" group viewcode , colorcode groups non-capturing one.

to match entries no colorcode , viewcode, made whole "container" non-capturing group optional adding ? quantifier.

enter image description here


Comments