cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1734
Views
3
Helpful
7
Replies

Escaping a forward slash in pattern

ysolan
Level 1
Level 1

Hi Team,

So far, I used the following pattern in the yang "[a-z0-9 -] *" for per example matching this string : switch01-dev001

Now, i'm looking for matching also forward slash /   

i changed the pattern to "[a-z0-9-/]*"  but i doesn't work. I would like to match the following string : switch01-dev/001


Do you have an idea ?


Regards,

Yannick

1 Accepted Solution

Accepted Solutions

You could use [a-z0-9/-]*

Placing the dash last within the brackets is a 40-year old trick. That ensures it's not treated as part of any character range.

/jan

View solution in original post

7 Replies 7

Chri_Erenti
Level 1
Level 1

Hi Yannick,

maybe you have to precede it with the backslash: "[a-z0-9-\/]*"

Best Regards

Christian

Hi Christian,

Thanks, I tried "[a-z0-9-\/]*" 


But i have this ouput after a make :

.yang:22: error: invalid pattern: "Bad Pattern"

.yang:22:20: warning: illegal character after \

Best Regards,

Yannick

Hello Yannick,

I think solution here is to escape "-" instead of "/".

Without "-", it looks pattern is working with forward slash.

    leaf dummy {

      type string {

        pattern "[a-z0-9/]+";

      }

    }

  admin@ncs(config)# ytest test dummy dev/001

  admin@ncs(config-ytest-test)#

If I add "-", it fails because "-" means a range.

      type string {

        pattern "[a-z0-9-/]+";

      }

  admin@ncs(config)# ytest test dummy switch01-dev/001

  ------------------------------------^

  syntax error: "switch01-dev/001" is an invalid value.

  admin@ncs(config)#

So, if I escape "-" then it works.

      type string {

        pattern "[a-z0-9\-/]+";

      }

  admin@ncs(config)# ytest test dummy switch01-dev/001

  admin@ncs(config-ytest-test)#

By the way, I prefer using "-" outside of [] such as follows.

      type string {

        pattern "[a-z0-9]+\-dev/[0-9]+";

      }

Hope this helps.

Best regards,

Hiro

Chri_Erenti
Level 1
Level 1

Thank you Hiro,

you're right!

"-" is a special character inside the square brackets. Outside it is not.

It should work also in this way: "[a-z0-9]+-dev/[0-9]+" or more generic: "[a-z0-9]+-*/*"

Let me know.

Best Regards

Christian

Hello Christian,

Indeed, "-" should work without escaping outside of square brackets as "[a-z0-9]+-dev/[0-9]+".

Thank you for the correction!

Best regards,

Hiro

Hi guys,

Thanks a lot,  you have helped me to better understand the structure.

My final generic pattern is : "[a-z0-9]*-*/*";


Best regards,

Yannick

You could use [a-z0-9/-]*

Placing the dash last within the brackets is a 40-year old trick. That ensures it's not treated as part of any character range.

/jan