xsd pattern not working with more than two regex separated by OR ( | ) -
i facing problem following xsd pattern ...
<xsd:pattern value="\d+(,\d+)*(,[*])|\d+(,\d+)*|\d+(,[*])(,\d+)+" />
in above pattern want allow user enter following patterns only:
1,2,3 1,*,3 1,2,*
but when try enter 1,2,* gives me following exception ...
unknown exception occurred while updating = input string: "*"
i not see wrong expression can improved , doing might prevent error. fault might in way results of regular expression used, not regular expression itself.
the regular expression given in question \d+(,\d+)*(,[*])|\d+(,\d+)*|\d+(,[*])(,\d+)+
.
testing in notepad++ v6.5.5 shows matches 3 example lines, expression not anchored start , end of line. adding 1 ^
, 1 $
not sufficient because of precedence of |
.
using expressions matches lines:
^(\d+(,\d+)*(,[*])|\d+(,\d+)*|\d+(,[*])(,\d+)+)$
could use ^\d+(,\d+)*(,[*])$|^\d+(,\d+)*$|^\d+(,[*])(,\d+)+$
seems harder read , understand.
within regular expression
- the
[*]
matches 1 character , might better written\*
. - the
(,[*])
matches , captures 2 character sequence,*
. there no point in capturing fixed in places. can replaced,\*
throughout. - each alternative start
\d+
, factored out. - the first 2 alternatives differ final `(,[*]) clause.
making these changes gives expression:
^\d+((,\d+)*(,\*)*|,\*(,\d+)+)$
the question shows patterns 2 commas regular expression matches lines multiple commas allows second or final value replaced *
. original expression matches
1,2,3 1,*,3 1,2,* 1,2,3,4,5 1,*,3,4,5 1,2,3,*,* 1,2,3,4,*
but not match
1,*,*,4,5 1,*,3,*,5 1,*,3,4,* 1,2,*,*,5 1,2,*,4,* 1,2,*,4,5 1,2,3,*,5
to match given inputs 2 commas use:
^\d+,(\d+,\d+|\d+,\*|\*,\d+)$
to match string many numbers separated commas 1 (but not first one) replaced *
use:
^\d+(,\d+)*(,\*)?(,\d+)*$
note ?
in line above. means match 0 or 1 occurrence of preceding item. allows ,*
occur once in string.
Comments
Post a Comment