So I have a URL that could be in the following three formats:
https://www.example.com/d/abcd-1234/edit
https://www.example.com/d/abcd-1234/
https://www.example.com/d/abcd-1234
I would like to extract only the abcd-1234
bit from the above URLs. I've tried doing so using the following regular expression, however it will only capture the first and second case.
import re
id = re.search(r'https://www.example.com/d/(.+?)/', url).group(1)
For url=https://www.example.com/d/abcd-1234
the above will fail with:
AttributeError: 'NoneType' object has no attribute 'group'
given that it does not match the regex.
What regex shall I use in order to indicate that the part of interest could be followed by no character at all?