How to escape special characters of a string with single backslashes
This is one way to do it (in Python 3.x):
escaped = a_string.translate(str.maketrans({"-": r"\-",
"]": r"\]",
"\\": r"\\",
"^": r"\^",
"$": r"\$",
"*": r"\*",
".": r"\."}))
For reference, for escaping strings to use in regex:
import re
escaped = re.escape(a_string)
In Python, how do I have a single backslash element in a list?
When printing a list in python the repr() method is called in the background. This means that print(list_of_strings) is essentially the same thing (besides the newlines) as:
>>> for element in list_of_strings:
... print(element.__repr__())
...
'<'
'>'
'€'
'£'
'$'
'¥'
'¤'
'\\'
In actuality the string stored is '\' it's just represented as '\\'
>>> for element in list_of_strings:
... print(element)
...
<
>
€
£
$
¥
¤
\
If you print out every element individually as above it will show you the literal value as opposed to the represented value.
Add escape \ in front of special character for a string
Decide which special characters you want to escape and just call
query.replace("}", "\\}")
You may keep all special characters you allow in some array then iterate it and replace the occurrences as exemplified.
This method replaces all regex meta characters.
public String escapeMetaCharacters(String inputString){
final String[] metaCharacters = {"\\","^","$","{","}","[","]","(",")",".","*","+","?","|","<",">","-","&","%"};
for (int i = 0 ; i < metaCharacters.length ; i++){
if(inputString.contains(metaCharacters[i])){
inputString = inputString.replace(metaCharacters[i],"\\"+metaCharacters[i]);
}
}
return inputString;
}
You could use it as query=escapeMetaCharacters(query);
Don't think that any library you would find would do anything more than that. At best it defines a complete list of specialCharacters.
Escape special characters in a Python string
Use re.escape
>>> import re
>>> re.escape(r'\ a.*$')
'\\\\\\ a\\.\\*\\$'
>>> print(re.escape(r'\ a.*$'))
\\\ a\.\*\$
>>> re.escape('www.stackoverflow.com')
'www\\.stackoverflow\\.com'
>>> print(re.escape('www.stackoverflow.com'))
www\.stackoverflow\.com
Repeating it here:
re.escape(string)
Return string with all non-alphanumerics backslashed; this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.
As of Python 3.7 re.escape()
was changed to escape only characters which are meaningful to regex operations.
Replace escape characters like \n \t etc with \\t , \\n
I found re.escape
as pointed to by Karoly Horvath. This is how it works.
>>> re.escape('ads;lfkjaldsf\ndsklajflad\tkjhklajf\n')
'ads\\;lfkjaldsf\\\ndsklajflad\\\tkjhklajf\\\n'
Update:
While I see re.escape
escapes a lot too much. Spaces , semicolons and lot many characters which don't need to be escaped in my case.
>>> re.sub(r'(\n|\t|\"|\')',lambda m:{'\n':'\\n','\t':'\\t','\'':'\\\'','\"':'\\\"'}[m.group()], "hello hi \n \'GM\' \t TC \n \"Bye\" \t")
'hello hi \\n \\\'GM\\\' \\t TC \\n \\"Bye\\" \\t'
This is what I figured out which really helped.
Escape characters in raw Python string
Quoting the documentation:
When an
'r'
or'R'
prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string. For example, the string literalr"\n"
consists of two characters: a backslash and a lowercase'n'
. String quotes can be escaped with a backslash, but the backslash remains in the string; for example,r"\""
is a valid string literal consisting of two characters: a backslash and a double quote;r"\"
is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character).
Added emphasis mine.
Raw strings thus do attach some meaning to a backslash, but only where quotes are concerned.
Replace backslash to double backslash in special character
We interpret the problem as having an input character string containing unicode and we want to show the unicode as escaped symbols instead.
Use
stringi::stri_escape_unicode(x)
For example if x
is a single unicode character then this will give a 6 character string whose first character is backslash, second is u
and next 4 are digits.
x <- "\u2265"
nchar(x)
## [1] 1
cat(x, "\n")
## ≥
y <- stringi::stri_escape_unicode(x)
nchar(y)
## [1] 6
cat(y, "\n")
## \u2265
Removing single backslash from string
You report that the API is returning "Moe\'s Restaurant & Brewhouse"
. More than likely you are looking at a Swift dictionary or something like that and it is showing you the string literal representation of that string. But depending upon how you're printing that, the string most likely does not contain any backslash.
Consider the following:
let string = "Moe's"
let dictionary = ["name": string]
print(dictionary)
That will print:
["name": "Moe\'s"]
It is just showing the "string literal" representation. As the documentation says:
String literals can include the following special characters:
- The escaped special characters
\0
(null character),\\
(backslash),\t
(horizontal tab),\n
(line feed),\r
(carriage return),\"
(double quote) and\'
(single quote)- An arbitrary Unicode scalar, written as
\u{n}
, wheren
is a1
–8
digit hexadecimal number with a value equal to a valid Unicode code point
But, note, that backslash before the '
in Moe\'s
is not part of the string, but rather just an artifact of printing a string literal with an escapable character in it.
If you do:
let string2 = dictionary["name"]!
print(string2)
It will show you that there is actually no backslash there:
Moe's
Likewise, if you check the number of characters:
print(dictionary["name"]!.characters.count)
It will correctly report that there are only five characters, not six.
(For what it's worth, I think Apple has made this far more confusing than is necessary because it sometimes prints strings as if they were string literals with backslashes, and other times as the true underlying string. And to add to the confusion, the single quote character can be escaped in a string literal, but doesn't have to be.)
Note, if your string really did have a backslash in it, you are correct that this is the correct way to remove it:
someString.stringByReplacingOccurrencesOfString("\\", withString: "")
But in this case, I suspect that the backslash that you are seeing is an artifact of how you're displaying it rather than an actual backslash in the underlying string.
Use preg_replace() to add two backslashes before each match
Welcome to the joys of "leaning toothpick syndrome" - backslash is such a commonly used escape character that it frequently requires escaping multiple times. Let's have a look at your case:
- Required output (presumably because of some other escaping context):
\\
- Escape each
\
with an additional\
for use in the PCRE regex engine:\\\\
- Escape each
\
there for use in a PHP string:\\\\\\\\
$value = 'mercedes-benz';
$pattern = '/(\+|-|\/|&&|\|\||!|\(|\)|\{|}|\[|]|\^|"|~|\*|\?|:|\\\)/';
$replace = '\\\\\\\\${1}';
echo preg_replace($pattern, $replace, $value);
As mickmackusa points out, you can get away with six rather than eight backslashes in some cases, such as a replacement of '\\\\\\'
; this works because the regex engine sees \\\
, which is an escaped backslash (\\
) followed by a single backslash (\
) that can't be escaping anything because it's the end of the string. Simply doubling for each "layer" of escaping is probably safer than learning when this short-cut is and isn't valid, though.
is it possible to escape character within single quotes in ruby?
The only characters that needs to be escaped in a single quoted string are '\\'
(for backslash \
) and '\''
(for single quote '
itself).
Related Topics
How Include Static Files to Setuptools - Python Package
Separation of Business Logic and Data Access in Django
Accessing Mp3 Metadata with Python
How to I Lazily Read Multiple JSON Values from a File/Stream in Python
Python Pip Install Module Is Not Found. How to Link Python to Pip Location
Want to Find Contours -> Valueerror: Not Enough Values to Unpack (Expected 3, Got 2), This Appears
Why Do Some Functions Have Underscores "_" Before and After the Function Name
Difference Between Numpy Dot() and Python 3.5+ Matrix Multiplication @
Matplotlib Xticks Not Lining Up with Histogram
How to Convert Columns into One Datetime Column in Pandas
Does a File Object Automatically Close When Its Reference Count Hits Zero
What Is _Future_ in Python Used for and How/When to Use It, and How It Works
Convert Rgba Png to Rgb with Pil
How to Apply Piecewise Linear Fit in Python