I am very new to Python, this is actually my first script.
I am struggling with Python regular expressions. In particular re.sub()
I have the following code:
variableTest = "192" test = re.sub(r'(\$\{\d{1,2}\:)example.com(\})', r'\1' + variableTest + r'\2', searchString, re.M )
With this I am trying to match something like host": "${9:example.com}" inside searchString and replace example.com with the server name or IP address.
If variableTest contains an IP, it fails. I get the following error: sre_constants.error: invalid group reference
I tested it with variableTest equal to "127.0.0.1", "1", "192", "192.168". "127.0.0.1" works, but the rest does not. If I add another letter, it also works.
variableTest is a string - checked with type(variableTest)
I completely lost why this is so.
If I delete r'\1' in the replacement string, it also works. r'\1' will contain t ${\d}: with \d number from 1 to 999.
Any help would be greatly appreciated!
tone7 source share