Question

I spend a couple of hour with no good result (maybe my mood is not helping about it).

I am trying to build a regex to help me match both urls:

/reservables/imagenes/4/editar/6
/reservables/imagenes/4/subir

As you note above, the last segment in the first url 6 is not present at the end of the second url, because this segments is optional here. So I need to match both urls in one regex, for that, I have tried this:

reservables/(editar|imagenes)/([0-9]+)/(imagen|editar|actualizar|subir)/([0-9]+)

That works fine only for the first url. So, reading a few notes about regex it suggest me that I need the ? symbol, right? So, I tried this one, but it did not work:

reservables/(editar|imagenes)/([0-9]+)/(imagen|editar|actualizar|subir)/([0-9]+)?

Well, I do not what I am doing wrong.

Was it helpful?

Solution

You want to put the ? around the / as well, like so:

reservables/(editar|imagenes)/([0-9]+)/(imagen|editar|actualizar|subir)(?:/([0-9]+))?

You can see that it matches correctly on debuggex.

OTHER TIPS

This one will work:

reservables/(editar|imagenes)/([0-9]+)/(imagen|editar|actualizar|subir)/([0-9]*)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top