How to re.sub() a optional matching group using regex in Python?
Question: 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.


Answer: 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`](https://pypi.python.org/pypi/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'
    

