Home > Enterprise >  Can't figure how to escape literal dot in Python
Can't figure how to escape literal dot in Python

Time:02-21

Good night, fellows!

Working around with Python I have a text with many sequences like the one below:

\n34.99

which I would like to change to

\t34.99

Trying with re.sub('\n\d \\.\d ', '\t\d \\.\d ', text), where text is the variable holding the text to replace, I get an error in Python 3.9.10

re.error: bad escape \d at position 1

Can you guys help me make my mind up?

CodePudding user response:

I would get rid of use the regex, due to it performance. In your case, this happened because '\n\d \\.\d ' is a formatted string, you should make it as raw string.

So the code would be:

re.sub(r'\n\d \\.\d ', r'\t\d \\.\d ', text)

CodePudding user response:

The replacement input into re.sub does not accept regular expressions, but rather only string literals with possible capture groups as well. I would use a capture group here:

inp = "Hello\n34.99"
print("Input: "   inp)
output = re.sub(r'\n(\d (?:\.\d )?)', r'\t\1', inp)
print("Output: "   output)

This prints:

Input: Hello
34.99
Output: Hello   34.99

CodePudding user response:

you can use this reg

re.sub(r'\n(\d \.\d )', r'\t\1',text)

\1meaning keep the first param child part

  • Related