Replace Forward Slash with Double Backslash Enclosed in Double Quotes

Replace forward slash with double backslash enclosed in double quotes

When / is part of a regular expression that you want to replace with the s (substitute) command of sed, you can use an other character instead of slash in the command's syntax, so you write, for example:

sed 's,/,\\\\,g'

above , was used instead of the usual slash to delimit two parameters of the s command: the regular expression describing part to be replaced and the string to be used as the replacement.

The above will replace every slash with two backslashes. A backslash is a special (quoting) character, so it must be quoted, here it's quoted with itself, that's why we need 4 backslashes to represent two backslashes.

$ echo /etc/passwd| sed 's,/,\\\\,g'
\\etc\\passwd

python replace single backslash with double backslash

No need to use str.replace or string.replace here, just convert that string to a raw string:

>>> strs = r"C:\Users\Josh\Desktop\20130216"
^
|
notice the 'r'

Below is the repr version of the above string, that's why you're seeing \\ here.
But, in fact the actual string contains just '\' not \\.

>>> strs
'C:\\Users\\Josh\\Desktop\\20130216'

>>> s = r"f\o"
>>> s #repr representation
'f\\o'
>>> len(s) #length is 3, as there's only one `'\'`
3

But when you're going to print this string you'll not get '\\' in the output.

>>> print strs
C:\Users\Josh\Desktop\20130216

If you want the string to show '\\' during print then use str.replace:

>>> new_strs = strs.replace('\\','\\\\')
>>> print new_strs
C:\\Users\\Josh\\Desktop\\20130216

repr version will now show \\\\:

>>> new_strs
'C:\\\\Users\\\\Josh\\\\Desktop\\\\20130216'

Regex replace slash with backslash inside #include double quotes

Your code is in C++, but you haven't said how you plan to replace the slashes: this could be in an IDE, with a script, or with other tools.

Here is a solution that will work Notepad++ (tested) using a search and replace in regex mode.

Search: (?m)(?:^#include\s*"|\G)[^/"]*\K/

Replace: \

Explanation:

(?m)(?:^#include\s*"|\G)[^/"]*\K/
  • Use these options for the whole regular expression (?m)
    • ^$ &match at line breaks m
  • Match the regular expression below (?:^#include\s*"|\G)
    • Match this alternative (attempting the next alternative only if this one fails) ^#include\s*"
      • Assert position at the beginning of a line (at beginning of the string or after a line break character) (line feed) ^
      • Match the character string “#include” literally (case sensitive) #include
      • Match a single character that is a “whitespace character” (ASCII space, tab, line feed, carriage return, vertical tab, form feed) \s*
        • Between zero and unlimited times, as many times as possible, giving back as needed (greedy) *
      • Match the character “"” literally "
    • Or match this alternative (the entire group fails if this one fails to match) \G
      • Assert position at the end of the previous match (the start of the string for the first attempt) \G
  • Match any single character NOT present in the list “/"” [^/"]*
    • Between zero and unlimited times, as many times as possible, giving back as needed (greedy) *
  • Keep the text matched so far out of the overall regex match \K
  • Match the character “/” literally /

java string replace a backslash double quote with a single quote

In Java Strings are immutable. What ever operation you perform on a String results in new object. You need to re-assign the value after operation. Following may help you.

entry = entry.replace("\\\"", "'");

Replace double quotes in text enclosed with double quotes in CSV file with back slash using PowerShell

Try

 -replace '(?<=\w)\"(?=\w)','\"'

Example

$tempfile = New-TemporaryFile

@'
"Column1","Column2","Column3"
"Te"st1","Tes"t2","Test"3"
'@ | Set-Content $tempfile

(Get-Content $tempfile) -replace '(?<=\w)\"(?=\w)','\"' | Set-Content $tempfile

Get-Content $tempfile

output

"Column1","Column2","Column3"
"Te\"st1","Tes\"t2","Test\"3"

The regex pattern uses a look ahead and look behind to only act upon double quotes that have a word character on both sides of it. You could adjust to accept more than just word characters adjacent. You could also go the opposite direction, for example only work on those that don't have a comma before/after.

python3 replacing double backslash with single backslash

Take a closer look at the string, they are all single slash.

In [26]: my_str[0]
Out[26]: '\\'

In [27]: my_str[1]
Out[27]: 'x'

In [28]: len(my_str[0])
Out[28]: 1

And my_str.replace('\\','\') won't work because the token here is \', which escapes ' and waits for the another closing '.

Use my_str.replace('\\', '') instead


Update: after few more days, I realize the following discussion may also be helpful. If the intension of a string with escape ('\\x' or '\\u') are eventually hex/unicode literals, they can be decoded by escape_decode.

import codecs
print(len(b'\x32'), b'\x32') # 1 hex literal, '\x32' == '2'
print(len(b'\\x32'), b'\\x32') # 4 chars including escapes
print(codecs.escape_decode('\\x32', 'hex')) # chars->literal, 4->1

# 1 b'2'
# 4 b'\\x32'
# (b'2', 4)

s = '\\xa5\\xc0\\xe6aK\\xf9\\x80\\xb1\\xc8*\x01\x12$\\xfbp\x1e(4\\xd6{;Z'
ed, _ = codecs.escape_decode(s, 'hex')
print(len(s), s)
print(len(ed), ed)

# 49 \xa5\xc0\xe6aK\xf9\x80\xb1\xc8*$\xfbp(4\xd6{;Z
# 22 b'\xa5\xc0\xe6aK\xf9\x80\xb1\xc8*\x01\x12$\xfbp\x1e(4\xd6{;Z'

json replace backslash to double quote

You can use the replace javascript function like this.

str.replace("\\", "\"");

If there are several \ in the string, use loop to replacing all the \.

If you are using CSharp, do like below.

str = str.Replace("\\", "\"");

Please have a try and let me know if it works or not.



Related Topics



Leave a reply



Submit