Search code examples
regexpcre

How to exclude specific uri in regex


I have a regular experession like this PRCE using php:

^/someurl/* There are a lot of urls like

/someurl/test
/someurl/something/{version}/{name}/{etc}

and i need to exclude urls like this one: ^/someurl/test/{version}/commands/*

{version} is a float number like 2.1.1 2.4

I've tried this ^((?!/someurl/test/[0-9].+/commands/*))

It works But I need to add this to single line like ^/someurl/* Excluding ^((?!/someurl/test/[0-9].+/commands/*))

How to join them? Thanks.


Solution

  • You may use

    ^/someurl/(?!test/[0-9.]+/commands/).*
    

    Or a bit more precise

    ^/someurl/(?!test/[0-9]+(?:\.[0-9]+)+/commands/).*
    

    See the regex demo

    Details

    • ^/someurl/ - /someurl/ at the start of the string
    • (?!test/[0-9.]+/commands/) - immediately to the right of the current position, there can't be test/, then 1+ digits or dots, then /commands/ substring (if [0-9]+(?:\.[0-9]+)+ is used it will match 1 or more digits, and then 1 or more repetitions of a dot followed with 1+ digits)
    • .* - any 0+ chars as many as possible.