I have url strings such as:
"https://facty.com/ailments/body/10-home-remedies-for-styes/pageid=2/slide_3/"
Now, I need to capture the slide_3
part, more specifically the start position of the digit 3
on constraint that it should be a single digit( neither preceded nor succeeded by any digit) not preceded by an "=". So, pageid=2
shouldn't match while slide_3
should.
I tried this with python regex:
p = re.compile('/.*(?<!=)(?<!\d)\d(?!\d).*/')
s = "https://facty.com/ailments/body/10-home-remedies-for-styes/pageid=2/slide_3/"
for m in p.finditer(s):
print(m.start(), m.group())
and the result is
6 //facty.com/ailments/body/10-home-remedies-for-styes/pageid=2/slide_3/
I understand why I get this, the first and the last "/" satisfy the regexp but so does the substring "/slide_3/".
How do I make sure I get the smallest substring that matches the regex.
Why doesn't this work:
'/[^/](?<!=)(?<!\d)\d(?!\d).*/'
Non greedy operator .*?
does not seem to do the trick since it does not guarantee the shortest possible match.
Strings that should match:
"https://facty.com/ailments/body/10-home-remedies-for-styes/pageid=2/slide_3/"
"https://facty.com/ailments/body/10-home-remedies-for-styes/pageid=2/sno3/"
"https://facty.com/ailments/body/10-home-remedies-for-styes/pageid=2/3/"
and the matches should be slide_3 , sno3, 3 respectively
Strings which shouldn't:
"https://facty.com/ailments/body/10-home-remedies-for-styes/pageid=2/slide/"
"https://facty.com/ailments/body/10-home-remedies-for-styes/pageid=2/slide_33/"
"https://facty.com/ailments/body/10-home-remedies-for-styes/pageid=2/33/"