1

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 / /

Roberto Pezzali
  • 2,484
  • 2
  • 27
  • 56
  • `(?<=prodotti\/)\d+`? – devnull Mar 27 '14 at 09:03
  • Use a lookahead instead of a lookbehind, also use a reluctant quantifier instead of a greedy one `(?<=prodotti\/).*?(?=\/)`. [See the difference](http://stackoverflow.com/q/5319840) – HamZa Mar 27 '14 at 09:04

1 Answers1

2

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.
ccjmne
  • 9,333
  • 3
  • 47
  • 62