Search code examples
python-3.xregexcapturepython-re

escape backslash in raw string which contains a capture group in Python with re.sub


I want to write a function that replaces IP-addresses with a LaTeX command to link the IP-address to a terminal for ssh connection. It would work if there would not be this backslash at the start of the replacing string. I tried the following:

import re

def link_ips(content: str) -> str:
  ip_regex = r'((?:[0-9]{1,3}\.){3}[0-9]{1,3})'
  return re.sub(ip_regex, r'\href{ssh://\1}{\1} ', content)

print(link_ips('192.168.178.1'))

And I got the error

re.error: bad escape \h at position 0

I expected \href{ssh://192.168.178.1}{192.168.178.1}

I tried different ways of escape combination but I couldn't fix it. Do you have an idea how to fix it?


Solution

  • Try this:

    import re
    
    def link_ips(content: str) -> str:
        ip_regex = r'((?:[0-9]{1,3}\.){3}[0-9]{1,3})'
        return re.sub(ip_regex, r'\\href{ssh://\1}{\1} ', content)
    
    print(link_ips('192.168.178.1'))
    

    in programming languages backslash used to escape special characters like \t or \n so if you need literally a real backslash in your string you must escape that backslash too