Regex to filter '*' and '?' specia

2019-08-23 10:59发布

问题:

I am trying to create a regex in yang model to not allow * and ? characters. String * and ? should not be allowed as input. .e.g. - abc* - should be okay - * - is not okay and should be rejected Similarly string ("?") should be rejected. Tried my hand with regex '[^?]+' which is rejecting any string with any occurrence of * and ?. .e.g abc*, *abc, * and ? all of them are rejected.

回答1:

YANG uses the XML Schema (XSD) flavor of regular expressions, but this case would be similar in most flavors. If I understand correctly, you wish to prohibit a string to start with characters * and ?.

[^*?].*

The above says: the string always has at least one character, where the first character may be any character except * or ? and may be followed by any number of arbitrary characters.

You can read more about the specifics of YANG regular expressions here. Do note that there are subtle differences between regexes defined in different versions of XSD Schema and that YANG relies on the one defined in normative references section of RFC7950 (and RFC6020).