Reputation: 2494
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
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:
(?<=\/)
) by its matching lookahead... so it asserts that we can match a /
AFTER the last character consumed by .*
.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.http://www.demo.it/prodotti/822/Panasonic/TXP46G20E.html
, it would have matched 822/Panasonic
.Upvotes: 2