Reputation: 19627
My problem is quite simple.
I have a URL, sometimes it ends with specific characters. If they are present, I would like to add them to my new URL.
test1 = "url#123"
test2 = "url"
r = re.sub(r"url(#[0-9]+)?", r"new_url\1", test1)
# Expected result: "new_url#123"
# Actual result: "new_url#123"
r = re.sub(r"url(#[0-9]+)?", r"new_url\1", test2)
# Expected result: "new_url"
# Actual result: "error: unmatched group"
Of course, I can not just do re.sub("url", "new_url", test)
, because for example it could be "url/123" and in this case I do not wish to make amendments.
Upvotes: 10
Views: 2994
Reputation: 32094
In [4]: re.sub(r"url(#[0-9]+|$)", r"new_url\1", test1)
Out[4]: 'new_url#123'
In [5]: re.sub(r"url(#[0-9]+|$)", r"new_url\1", test2)
Out[5]: 'new_url'
Upvotes: 0
Reputation: 369074
You cannot use optional matching group in replacement string.
How about following approach?
>>> import re
>>> test1 = "url#123"
>>> test2 = "url"
>>> re.sub(r"url((?:#[0-9]+)?)", r"new_url\1", test1)
new_url#123
>>> re.sub(r"url((?:#[0-9]+)?)", r"new_url\1", test2)
new_url
BTW, if you use regex
, you can use optional matching group:
>>> import regex
>>> test1 = "url#123"
>>> test2 = "url"
>>> regex.sub(r"url(#[0-9]+)?", r"new_url\1", test1)
'new_url#123'
>>> regex.sub(r"url(#[0-9]+)?", r"new_url\1", test2)
'new_url'
Upvotes: 10