Multiple regular expressions in a bar string

Asked

Viewed 435 times

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?

  • 1

    You have to "escape" that bar with "", and actually - also... Already you tested ^\/[a-z\-]\/?

  • 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.

  • 1

    /+$~ says your string should end with a bar.

  • 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.

  • @Sergio no need to escape the - if he is the last member of [] :D

  • @Guilhermelautert true, but it’s good to refer to those who don’t know.

Show 1 more comment

2 answers

5


The following expression solves my problem: ^\/[a-z\-]+\/[0-9]+\/?+$

I found the solution using this site which was suggested through Sergio together with a little learning on the site Regexone

I saved the solution on the link that can be tested: https://regex101.com/r/fC0gF4/1

  • 1

    I think you can simplify the regex like this #^/([\w-]+)(/\d+)?$#

  • 1

    Thanks @rray I will test, I do not understand very well of regex. In my project I gave a slight simplified using another delimiter, ~^/[(a-zA-Z0-9-)]+/[(0-9)]+/?+$~

  • Can make the expression a case insensitive, with the modifier i of PCRE just put it past the delimiter, ~i, already simplifies a little :D

  • if you have no problem with the _ can change to ~^/[\w-]+/\d+/?+$~

  • 1

    Note: as you can see on Regex101, groups do not work within [], then while doing [(0-9)] you’re saying you accept them too () parentheses.

  • Thanks, I forgot to update the link.

  • \w also house numbers, so it is not a good one when you want only letters. One can make a "double negation", like [^\W\d_], but not only is it hard to read how much in the case can’t match with -, so in my opinion the explicit form is even better (as it is in the answer). P.S. What do you mean by ?+ in the end? I know +?, but I don’t know what that combination does. It wouldn’t just be ??

  • Forget it, I figured it out what the ?+ ago.

Show 3 more comments

4

Your mistake was putting the + in the wrong place:

URL: /testando-minha-url/
REGEX: ~^/[a-z-]/+$~
SAÍDA: false

That’s saying marry a bar, then a single character which is letters or dash, followed by one or more bars. Placing the + at the right place should solve your problem:

URL: /testando-minha-url/
REGEX: ~^/[a-z-]+/$~

This matches a slash, followed by one or more characters that are letters or dash, followed by a slash at the end.

From there it’s just doing the same thing to the numbers by putting a ? on the last bar for it to be optional (as you have already discovered):

~^/[a-z-]+/\d+/?$~
  • And the part to capture the numbers at the end, if any?

  • 1

    @rray I was just commenting on the attempted solution of the AP, since no one had pointed it out. I did not find it necessary to go to the end, since he had already found an answer. But I can complete, if you think it important.

  • A no, no problem, it was not to piss off his answer, I was curious to know if he had an alternative regex.

  • 1

    @rray hehe no, in this case I do not see much room for improvement (because the characters are mutually exclusive), the AP solution is satisfactory.

  • Thanks for the reply @mgibsonbr, these regex still give me a lot of headache when I need to use them, need to train or find some good reference.

  • 1

    @Renancavalieri Do you know the site regular-Expressions.info (in English)? It’s the main reference I use, explains every detail of the regexes however obscure, and even compares their implementation in different languages and platforms.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.