Delgan
Delgan

Reputation: 19627

How to re.sub() a optional matching group using regex in Python?

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

Answers (3)

sureshvv
sureshvv

Reputation: 4422

re.sub(r'url(#*.*)', r'url\1', test1)

Upvotes: 0

newtover
newtover

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

falsetru
falsetru

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

Related Questions