c# - Validating property using RegularExpression attribute with regex conditional always passes -


i'm trying use conditionals in regularexpression attribute validate particular set of formats property, seems include (conditional) in pattern passes validation.

i tried simplest ones, , examples microsoft (like one: @"\b(?(\d{2}-)\d{2}-\d{7}|\d{3}-\d{2}-\d{4})\b" nothing seems work (all values allowed).

added clarity: need validate tax registration number. rules are:

if starts country identifier     country cc strict format check    other countries allow string else    assume it's local number , strict format check 

(most of time registration local , rules known, should possible enter foreign number unknown format).

while grateful regex allowing above, question not particular pattern itself, but rather why using conditional - in particular scenario (asp.net/c# attribute) - in pattern trips off validation , how resolve it.

the syntax condititional statement (?(?=condition)iftrue|else), regex should be

\b(?(?=\d{2}-)\d{2}-\d{7}|\d{3}-\d{2}-\d{4})\b 

but seems me don't need condition in example, remember (?=...) 0 width (and here conditions mutually exclusive).this should enough:

\b(?:\d{2}-\d{7}|\d{3}-\d{2}-\d{4})\b 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -