ConfD User Community

YANG patteren to allow string which does not contain '&' character?

I want to allow a string which does not contain ‘&’ character, how it can be done using YANG pattern ?

pattern ‘[a-zA-Z0-9~`@#$%^*()-_=+/>.<,;}{[]]+’; // I might have missed some char which are not in my keyboard.

With that method, you need to \-escape [ and ] (w3c regexps can have “nested” “character classes”), i.e.

pattern '[a-zA-Z0-9~`@#$%^*()-_=+/>.<,;}{\[\]]+';

If you use YANG 1.1, it has a modifier substatement to pattern - see https://tools.ietf.org/html/rfc7950#section-9.4.6 - which makes this kind of restriction much easier to specify:

pattern '.*&.*' {
  modifier invert-match;
}

Think of grep -v if you are familiar with the Unix command-line - i.e. the string must not match the pattern in this case.

1 Like

Thank you for your reply and quick response.

Actually, after posting I realized that there is a much simpler and “safer” solution - just use “longer ranges”:

pattern "[ -%'-~]+";

This will allow all printable ascii characters except & (which is the one between % and '). You can of course tweak it a bit if you don’t actually want to include e.g. space and ’ (they weren’t in your original pattern, but I’m sure they are on your keyboard:-). Just look at an ascii table to see the full order of the characters - you can typically get one with ‘man ascii’ on Unix/Linux.