Yang pattern not accepting regex containing ^ and $

552 views Asked by At

Yang pattern does not accept valid Regex when it contains caret ^ or dollar $.

The bellow pattern is valid on Regex validators, but when run inside a Yang pattern template in an is-types.yang file my application errors.

However when i take off the ^ and $ from each part of the expression it works fine. However this now allows the regex to accept unwanted values.

I am trying to match either 1, 11, 1/1/1, 1/1/1/1.

Trying the pattern:

^([0-9]{1,3}/[0-9]{1,3}/[0-9]{1,3})$|^([0-9]{1,3}/[0-9]{1,3}/[0-9]{1,3}/[0-9]{1,3})$|^([0-9]{1,2})$

Yang typedef:

typedef interface_number_value {
    type string {
        pattern "([0-9]{1,3}/[0-9]{1,3}/[0-9]{1,3})|([0-9]{1,3}/[0-9]{1,3}/[0-9]{1,3}/[0-9]{1,3})|([0-9]{1,2})" {
            error-message "error";
        }
    } 
    description
        "description";
}

Also I am not sure where to raise issues/bugs with Yang.

1

There are 1 answers

0
predi On BEST ANSWER

This is what RFC7950 says about the "pattern" statement:

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]. It is used to restrict the built-in type "string", or types derived from "string", to values that match the pattern.

If you follow the normative reference XSD-TYPES, you eventually get to:

[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.

Therefore the regular expressions in YANG "pattern" statements are defined by XML Schema (XSD) type specs (and that specific revision of them for YANG 1.0 and 1.1).

Unlike some other regex flavors, a ^ character at the beginning of a YANG "pattern" or a $ at the end of a YANG "pattern" are not interpreted as anchors. Instead they are interpreted literally as characters that must match. All "pattern" expressions are implicitly anchored and therefore match the entire value.

You can find out more about the specifics of XML Schema Regular Expressions here.