0

I have text for fuzzy searching where user gives text, and I replace with characters used for regex parsing later.

Here is the text saved in configuration_blocks variable:

neighbor 10.1.(()).(())

Here is the code:

configuration_blocks = [sub.replace("$", ".*").replace("(())", "\S+") for sub in configuration_blocks]

However, the result is neighbor 10.1.\ \S with two backslashes.

How can I do this to get one backslash only?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
hfakoor222
  • 25
  • 7

1 Answers1

-1

If you want to use a single backslash in your string you need to escape it will another backslash like \. So in your code you do this:

configuration_blocks = [sub.replace("$", ".*").replace("(())", "\S+") for sub in configuration_blocks]

you might still get double backslash in the console but in the regex context, it will behave as the single backslash.

if you want to see the string as a single backslash in the console use raw text like this (r"")

Super MaxLv4
  • 148
  • 9