To Include additional escape characters in pattern

Hi ,
I current have an existing pattern like mentioned below.

type string {
pattern ‘[a-zA-Z0-9#$%&()*+,.:/<=>@^_{}~^[]\-]+’ {
error-message “${DICT_YANG_ERROR_PATTERN_PASSWORD}”;
}
Now, I would like to add additional characters which include escape characters like single quote double quote question mark and few others mentioned below.

!’;?"’|

How can i include all in one single pattern check.
I tried using single and double back slash and also tried to concatenate like pattern ‘stringset1’ + ‘stringset2’ { error string},but didn’t work.
Kindly help how i can be able to include all the below mentioned in one pattern check and any relevant documents.

As far as I recall, none of the characters you want to add are “special” in a “Character Group” (the thing inside [ ] in a regular expression), i.e. there is no need to escape them per se. However the [ and ] characters are special (w3c regexps can have “nested” “Character Class Expressions”), i.e. they need to be escaped already in your original pattern.

And of course single quote is “problematic” since you are (wisely) using them for the YANG quoting of the string (as you can see in https://tools.ietf.org/html/rfc7950#section-6.1.3, a single quote can never appear inside a single-quoted string) - but this is most easily handled with “quote switching”. So, your actual pattern can be

[a-zA-Z0-9#$%&()*+,.:/<=>@^_{}~^\[\]\-!;?"|']+

and to properly quote this for YANG, you can use

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

However since it seems you want to include all printable ASCII characters (except space), this might be more easily expressed as

pattern '[!-~]+';

If there are some of those characters you want to exclude, you can always “split the range” and skip them that way,

1 Like

Thanks a lot for resolving the issue.
By the way , can you show some pointers to understand the regex of pattern.
And also explain in brief on what pattern ‘[!-~]+’; does.

Thanks,
Naveen

This is of course fully specified in the YANG RFC(s) - e.g. RFC 7950 9.4.5. The “pattern” Statement says

   The "pattern" statement, which is an optional substatement to the
   "type" statement, takes as an argument a regular expression string,
   as defined in [XSD-TYPES].

where [XSD-TYPES] is a reference to (in the RFC)

   [XSD-TYPES]
              Biron, P. and A. Malhotra, "XML Schema Part 2: Datatypes
              Second Edition", World Wide Web Consortium Recommendation
              REC-xmlschema-2-20041028, October 2004,
              <http://www.w3.org/TR/2004/REC-xmlschema-2-20041028>.

In that document, you will find the specification of regular expressions in appendix F Regular Expressions. Admittedly it’s pretty heavy reading - you might find more “popular” descriptions by googling e.g. “w3c regular expression”.

I assume you know what the […]+ means, since you were already using it - the !-~ is a range of characters, just like A-Z. To find out which characters are included in the range, you need to look at a table of the ASCII characters, can be found with google or with man ascii on a Linux/Unix system. There you will find that ‘!’ (decimal code 33) is the first printable character (excluding space (decimal code 32)), and ‘~’ (decimal code 126) is the last printable character.

Thanks a lot for all your help and support. Appreciate it.

Hi,

Based on your suggestion,The above pattern works well when i added pattern check like
pattern ‘[!-~]+’;
However, i would like to include CRLF and newline character(ASCII char 10 and 13) to existing set.Can you kindly let me know how this can be achieved.

As described in the document I referred you to, they are represented as \n and \r, respectively. So just add them to the set: '[!-~\n\r]+’

Thank you very much.