There is a regular season:

/\/tickets\/(.*)\/(.*)\//i 

Which I use to recognize lines like this /tickets/moscow/saint-petersburg/ , but the problem is that the regular schedule can capture the line /tickets/moscow/tokyo/simferopol/ and so on. I solved this problem with the help of another regular program:

 path.match(/\//g).length == 4 

What regular in nginx do I need to use and how to correctly write it in order to catch only the correct 4-slash strings in nginx? Thank.

    1 answer 1

    1. Explicitly indicate that there should be nothing after the last slash:

       /\/tickets\/(.*)\/(.*)\/$/i 
    2. Explicitly mark the range of valid characters so as not to wrestle with greedy / lazy scanning. In addition, it is worth replacing quantifiers in order to prevent two consecutive slashes:

       /\/tickets\/([a-zA-Z0-9-]+)\/([a-zA-Z0-9-]+)\/$/i 

    This is enough to solve the problem. Here are the results of testing this expression in two of your examples: https://regex101.com/r/nq6b2Z/1/tests .