'Python Regex Expression Needed to Add Only a 2nd Backslash
I have an expression that works in sed and need to adopt it for python. I want to insert a backslash next to each "single" backslash. For clarity here, I am replacing an isolated backslash with an "X". Here is what works in sed. Remember that sed actually sees the input as aaa\bbb\ccc. I would like the output to be "aaa\bbb\ccc".
echo "aaa\\bbb\\\\ccc" | sed -e 's/\([^\\]\)\\\([^\\]\)/\1X\2/'
I tried a few things like:
re.sub("([^\\])\\([^\\])", "\1X\2", r"123\456\\789")
r"123\456\\789".replace(r"([^\])\([^\])", "\1X\2")
Solution 1:[1]
Brute force and I figured it out. I'll need to look at it some more to figure out why.
re.sub(r"([^\\])\\([^\\])", r"\1x\2", r"aaa\bbb\\ccc")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Stuart Rothrock |
