c# - match pattern for startswith -
i want regex match in c# check whether string starts part of pattern. if pattern "abc...ghi" valid strings can in format "a","ab","abcdef","abcxyxghi"
this sample code. regex has in pattern make work
string pattern = "abc...ghi" code = "a" //valid code = "abc" valid code = "abcde" //valid code = "abcxxx" //valid code = "abcxxxghi" //valid code = "abcxxxghiaa" //invalid code = "b" //invalid regex.ismatch(code, pattern)
you can use ?
, make optional part of regexp. final regexp string be
a(b(c(.(.(.(g(h(i?)?)?)?)?)?)?)?)?
the final string quite messy can create automatically
the visualization of above regexp here http://www.regexper.com/#a(b(c(.(.(.(g(h(i%3f)%3f)%3f)%3f)%3f)%3f)%3f)%3f)%3f
Comments
Post a Comment