Capture with regex certain part of the string

Asked

Viewed 61 times

2

In this sentence, /comprar/volkswagen/gol/11184529, I’d like to capture if the word exists comprar what comes next without the /, I would like to capture the word volkswagen (but it could be another brand(it would have to be generic, because I won’t guess which brand)) and gol (but it could be another model(it would have to be generic, because I won’t guess which brand))

  • 1

    Regex ^\/comprar\/([^\/]+)\/([^\/]+)\/\d+$. Dai takes the first and second groups.

1 answer

4


Regex ^\/comprar\/([^\/]+)\/([^\/]+)\/\d+$. Then take the first and the second group.

Tire ^ and $ if not just one occurrence per line.

  • 2

    That’s right, thank you

  • then I want to take all the parameters after the domain http://www.valeautoshopping.com.br/comprar/volkswagen/gol/11184529, how would you do? Alias the domain can also be another

  • Just use the regex logic I showed you: \/ to the bar, ([^\/]+) for the group you want to capture.

  • if I have 30 parameters, I have to repeat 30 times? example https://regex101.com/r/kz2ER9/2, but it always returns me as grupo 1 for each time you find and return me in js just the http and http:

  • Do https?:\/\/[^\/]+\/ and then repeat how many ([^\/]+) and \/ are the parameters and bars respectively, and take the group according to your need (does not need to be exactly the first and the second just because I said).

Browser other questions tagged

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