I’m writing an XML schema and need to prevent the text of an element from matching certain values. (Eg. the variableName element cannot match ‘int’, ‘byte’, ‘string’ etc.)
I have tried using a restriction with a pattern element similar to “^(int|byte|string)”, but without success.
Do you know the way to format the regular expression, or any other way to make this work?
After triple-checking that XML Schema (XSD) regexes really don’t support any of the features that would make this task easy (particularly lookaheads and anchors), I’ve come up with an approach that seems to work. I used free-spacing mode to make it easier to read, but that’s another feature the XSD flavor doesn’t support.
The first alternative matches anything that doesn’t start with the initial letter of any of the keywords. Each of the other top-level alternatives matches a string that starts with the same letter as one of the keywords but:
Note that XSD regexes don’t support explicit anchors (i.e.,
^,$,\A,\z), but all matches are implicitly anchored at both ends.One potential problem I can see: if the list of keywords is long, you might run up against a limit on the sheer length of the regex.