Questions tagged [backslash]

The backslash character \ (not to be confused with slash /) is used in many languages as an escape character to modify the meaning of the following character. It is also the directory separator in file paths under DOS and Windows.

The backslash character \ is a mirror version of the slash character /, created for use in computing. Its meaning depends on the context.

  • In many languages, \ followed by a character that has a special meaning makes the next character lose its special meaning. This is especially true inside string literals and regular expressions : for example, in many languages, "a\\b\"" represents the 4-character string a\b".
  • Sometimes \ gives the next character a special meaning. This is mostly the case when \ is followed by a letter or digit in a string literal, where it can be used to specify a control character or other special character. For example, \n specifies a newline in many languages, and \123 often specifies the character whose octal value is 123 (but note that some languages have different rules).
  • In DOS and Windows , the \ character is the directory (folder) separator in file paths. For example, c:\windows\cmd.exe is the file called cmd.exe in the directory called windows at the root of the c: drive.
  • In (La)TeX , \ starts a command name.
  • In Haskell , \ introduces a lambda expression .
991 questions
22
votes
3 answers

python replace backslashes to slashes

How can I escape the backslashes in the string: 'pictures\12761_1.jpg'? I know about raw string. How can I convert str to raw if I take 'pictures\12761_1.jpg' value from xml file for example?
Vyalov V.
  • 353
  • 1
  • 3
  • 13
22
votes
3 answers

Different behaviours of treating \ (backslash) in the url by FireFox and Chrome

BACKGROUND According to my experience when my ubuntu workstation is configured on domain with active directory, the user name created for me was according to the following pattern. domain_name\user_name Using the userdir extensions of apache on…
Shoaib Nawaz
  • 2,302
  • 4
  • 29
  • 38
21
votes
2 answers

How to handle backslash character in PowerShell -replace string operations?

I am using -replace to change a path from source to destination. However I am not sure how to handle the \ character. For example: $source = "\\somedir" $dest = "\\anotherdir" $test = "\\somedir\somefile" $destfile = $test -replace $source,…
timanderson
  • 843
  • 1
  • 10
  • 20
21
votes
3 answers

How to escape backslashes in R string

I'm writing strings which contain backslashes (\) to a file: x1 = "\\str" x2 = "\\\str" # Error: '\s' is an unrecognized escape in character string starting "\\\s" x2="\\\\str" write(file = 'test', c(x1, x2)) When I open the file named test, I…
Fnzh Xx
  • 661
  • 2
  • 8
  • 14
20
votes
6 answers

Replace single backslash with double backslash

It seems simple enough, right? Well, I don't know. Here's the code I'm trying: input = Regex.Replace(input, "\\", "\\\\\\"); However, I'm receiving an error, ArgumentException was unhandled - parsing "\" - Illegal \ at end of pattern. How do I do…
Johnny
  • 373
  • 1
  • 6
  • 11
20
votes
4 answers

Why isn't it possible to use backslashes inside the braces of f-strings? How can I work around the problem?

In Python >=3.6, f-strings can be used as a replacement for the str.format method. As a simple example, these are equivalent: '{} {}'.format(2+2, "hey") f'{2+2} {"hey"}' Disregarding format specifiers, I can basically move the positional arguments…
jmd_dk
  • 12,125
  • 9
  • 63
  • 94
19
votes
3 answers

Replace double backslashes with a single backslash in javascript

I have the following problem: I have a script that executes an AJAX request to a server, the server returns C:\backup\ in the preview. However, the response is "C:\\backup\\". Not really a big deal, since I just thought to replace the double…
Guido Visser
  • 2,209
  • 5
  • 28
  • 41
18
votes
2 answers

CMake: how to get the backslash literal in Regexp replace?

I am trying to replace forwardslashes with backslashes. To do that i have the following line of code: STRING(REGEX REPLACE "/" "\\" SourceGroup ${SourceGroupPath} ) SourceGroupPath = A/File/Path. SourceGroup is the variable to set the result…
WoutervD
  • 2,139
  • 2
  • 13
  • 13
17
votes
3 answers

How do I escape a series of backslashes in a bash printf?

The following script yielded an unexpected output: printf "escaped slash: \\ \n" printf "2 escaped slashes: \\\\ \n" printf "3 escaped slashes: \\\\\\ \n" printf "4 escaped slashes: \\\\\\\\ \n" Run as a bash script under Ubuntu 14, I see: escaped…
Charney Kaye
  • 3,667
  • 6
  • 41
  • 54
17
votes
2 answers

Escape double and single backslashes in a string in Ruby

I'm trying to access a network path in my ruby script on a windows platform in a format like this. \\servername\some windows share\folder 1\folder2\ Now If I try to use this as a path, it won't work. Single backslashes are not properly escaped for…
konung
  • 6,908
  • 6
  • 54
  • 79
16
votes
4 answers

How to replace " \ " with " \\ " in java

I tried to break the string into arrays and replace \ with \\ , but couldn't do it, also I tried String.replaceAll something like this ("\","\\");. I want to supply a path to JNI and it reads only in this way.
David Prun
  • 8,203
  • 16
  • 60
  • 86
15
votes
4 answers

Converting backslashes into forward slashes using javascript does not work properly?

I have a javascript variable comming from legacy system with backslashes into forward slashes: '/46\465531_Thumbnail.jpg' and I am trying to convert into this: '/46/465531_Thumbnail.jpg'. There is no way to fix the problem on the legacy…
Junior Mayhé
  • 16,144
  • 26
  • 115
  • 161
15
votes
3 answers

Python Regex escape operator \ in substitutions & raw strings

I don't understand the logic in the functioning of the scape operator \ in python regex together with r' of raw strings. Some help is appreciated. code: import re text=' esto .es 10 . er - 12 .23 with [ and.Other ] here is more ;…
JFerro
  • 3,203
  • 7
  • 35
  • 88
14
votes
4 answers

Golang : Escaping single quotes

Is there a way to escape single quotes in go? The following: str := "I'm Bob, and I'm 25." str = strings.Replace(str, "'", "\'", -1) Gives the error: unknown escape sequence: ' I would like str to be "I\'m Bob, and I\'m 25."
A.D
  • 2,150
  • 3
  • 13
  • 19
14
votes
6 answers

Add backslash to string

I have a path and I want to add to it some new sub folder named test. Please help me find out how to do that. My code is : string path = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); Console.WriteLine(path+"\test"); The result…
misha312
  • 1,443
  • 4
  • 18
  • 27
1 2
3
65 66