0

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?

Droider
  • 131
  • 12
  • You need to escape the backslash: `\\href` – Barmar Nov 12 '20 at 20:25
  • Using a raw string prevents the escape from being interpreted as part of the string literal, but you also have to protect it from `re` processing. – Barmar Nov 12 '20 at 20:26

1 Answers1

0

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

Alireza
  • 2,103
  • 1
  • 6
  • 19
  • Now I'm confused. I alread tried this solution and nearly every combination of backslashes and without raw string but without success. If you correct your indentation ( I couldn't) I can mark it as answer. – Droider Nov 13 '20 at 11:55