Home > Net >  regex matching multiple urls
regex matching multiple urls

Time:09-24

I am trying to match multiple urls with request types:

for example, for endpoints:

GET:/shop/australia/sydney/admin
GET:/shop/australia/sydney/read
GET:/shop/australia/sydney/customer/1
PATCH:/shop/australia/sydney/customer/1
DEL:/shop/australia/sydney/customer/1

for a single url, this works fine:

(GET:\/shop\/australia\/sydney\/admin(\/)?

but i need a regex to match all the above ones. i tried something like:

(GET:\/shop\/australia\/sydney\/admin(\/)?|GET:\/shop\/australia\/sydney\/read(\/)?)

but it isn't really working for me.

CodePudding user response:

You don't need to escape / in Python regexp, since it's not a delimiter.

You don't need to put a group around a single character when quantifying it, so (\/)? can just be /?.

Try to find the common parts of the regexp so you don't have to keep repeating it in all the alternatives.

GET:/shop/australia/sydney/(?:admin|read)/?|(?:GET|PATCH|DEL):/shop/australia/sydney/customer/\d 

CodePudding user response:

This will work, but bear in mind that it's very general and will capture other patterns as well:

[A-Z]*:(\/\w )*(\/\d )*

If you really want something more specific you can try something like this:

[A-Z]*:\/shop\/australia\/sydney\/\w (\/\d )*

  • Related