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

2
  • (?<=prodotti\/)\d+? Commented Mar 27, 2014 at 9:03
  • Use a lookahead instead of a lookbehind, also use a reluctant quantifier instead of a greedy one (?<=prodotti\/).*?(?=\/). See the difference Commented Mar 27, 2014 at 9:04

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

4 Comments

Can you add a brief explanation... ? :)
now is ok. I'm learning regex so I want also to know what's my fault :)
Hey, you're right to ask, and actually my answer had no explanation before I edited it (within a very short time), so that was totally legitimate ;)
Don't forget to accept the answer it your problem is solved :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.