6
I need a regular expression that matches the end of a URL in the following format:
/somente-letras/somente-numeros[/]
To match the part of somente-letras
, I used ~^/[A-Z]+$~
, but from the moment I put a bar, then nothing else works.
The tests I did:
URL: /testando-minha-url
REGEX: ~^/[a-z-]+$~
SAÍDA: true
Placing a bar at the end of the URL
URL: /testando-minha-url/
REGEX: ~^/[a-z-]+$~
SAÍDA: false
Putting a bar in the expression
URL: /testando-minha-url/
REGEX: ~^/[a-z-]/+$~
SAÍDA: false
That is, my problem is from the bar (forward Slash).
After the bar, I need you to match (match) only numbers, and possibly whatever final bar is optional, work with or without it.
I’m using the function preg_math
of PHP, preg_match($regex, $str)
Is it possible? Where am I going wrong?
You have to "escape" that bar with "", and actually
-
also... Already you tested^\/[a-z\-]\/
?– Sergio
I didn’t escape because I’m wearing
~
as delimiter, however I tried what you suggested, also did not work, also I tried this way/^\/[a-z\-]\/+$/
but I didn’t get any results.– Renan Cavalieri
/+$~
says your string should end with a bar.– rray
A regex:
^\/[(a-z\-)]+\/[(0-9)]+$
worked for the following string in format/testando-minha-string/2785
, now how do I make the last bar optional? @Sergio this site is miraculous, helped me a lot to get this result.– Renan Cavalieri
@Sergio no need to escape the
-
if he is the last member of[]
:D– Guilherme Lautert
@Guilhermelautert true, but it’s good to refer to those who don’t know.
– Sergio