Roberto Pezzali
Roberto Pezzali

Reputation: 2494

Regex to match number specific number in a string

I'm trying to fix a regex I create.

I have an url like this:

http://www.demo.it/prodotti/822/Panasonic-TXP46G20E.html

and I have to match the product ID (822).

I write this regex

(?<=prodotti\/).*(?<=\/)

and the result is "822/"

My match is always a group of numbers between two / /

Upvotes: 1

Views: 53

Answers (1)

ccjmne
ccjmne

Reputation: 9618

You're almost there!

Simply use:

(?<=prodotti\/).*?(?=\/)

instead of:

(?<=prodotti\/).*(?<=\/)

And you're good ;)

See it working here on regex101.


I've actually just changed two things:

  1. replaced that lookbehind of yours ((?<=\/)) by its matching lookahead... so it asserts that we can match a / AFTER the last character consumed by .*.
  2. changed the greediness of your matching pattern, by using .*? instead of .*. Without that change, in case of an url that has several / following prodotti/, you wouldn't have stopped to the first one.
    i.e., given the input string: http://www.demo.it/prodotti/822/Panasonic/TXP46G20E.html, it would have matched 822/Panasonic.

Upvotes: 2

Related Questions