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

**URL:** <https://dmap-community.ductus.global/t/yang-patteren-to-allow-string-which-does-not-contain-character/2916>\
**Category:** YANG\
**Created:** [February 7, 2020, 10:09am UTC](https://dmap-community.ductus.global/t/yang-patteren-to-allow-string-which-does-not-contain-character/2916 "2020-02-07T10:09:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mahesh](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@mahesh](https://dmap-community.ductus.global/u/mahesh)\
**Post date:** [February 7, 2020, 10:09am UTC](https://dmap-community.ductus.global/t/yang-patteren-to-allow-string-which-does-not-contain-character/2916/1 "2020-02-07T10:09:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![per](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@per](https://dmap-community.ductus.global/u/per)\
**Post date:** [February 7, 2020, 1:26pm UTC](https://dmap-community.ductus.global/t/yang-patteren-to-allow-string-which-does-not-contain-character/2916/2 "2020-02-07T13:26:03Z")

</div>

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

```auto
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](https://tools.ietf.org/html/rfc7950#section-9.4.6) - which makes this kind of restriction much easier to specify:

```auto
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.

---

<div class="post-metadata">

**Author:** ![mahesh](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@mahesh](https://dmap-community.ductus.global/u/mahesh)\
**Post date:** [February 8, 2020, 9:44am UTC](https://dmap-community.ductus.global/t/yang-patteren-to-allow-string-which-does-not-contain-character/2916/3 "2020-02-08T09:44:56Z")

</div>

Thank you for your reply and quick response.

---

<div class="post-metadata">

**Author:** ![per](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@per](https://dmap-community.ductus.global/u/per)\
**Post date:** [February 8, 2020, 10:23am UTC](https://dmap-community.ductus.global/t/yang-patteren-to-allow-string-which-does-not-contain-character/2916/4 "2020-02-08T10:23:07Z")

</div>

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

```auto
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.
