Can I escape characters in variable names?
You can use backticks:
R> `a + b` <- 3
R> `a + b`
[1] 3
tmp <- data.frame(1:10, rnorm(10))
names(tmp) <- c("a+b", "c&d")
ggplot(tmp, aes(`a+b`, `c&d`)) + geom_point()
See also ?Quotes
.
Python escape '$' character in variable name
Python variable names and Python keyword argument names (which is what you actually need her) cannot contain $
. You may be able to use the **kwargs
syntax though:
client.pet.get_pets(**{"$limit": 10})
What are valid characters for Windows environment variable names and values?
About variable values: you can use most characters as variable values, including white space. If you use the special characters <, >, |, &, or ^, you must precede them with the escape character (^) or quotation marks. If you use quotation marks, they are included as part of the value because everything following the equal sign is taken as the value.
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx?mfr=true
Check section "Setting environment variables".
About variable names: in my opinion, for best compatibility with every application, you should limit yourself to letters, numbers, underscore (_) and minus (-).
I'm quite sure that all POSIX valid characters for files are ok, but I did not found any evidence of this.
Concerning variable names names we need to also accept parenthesis since %ProgramFiles(x86)%
is a well-known envar. From my experiments it seems that in addition to letters and digits characters, these characters are valid _(){}[]$*+-\/"#',;.@!?
and these characters are not valid %<>^&|=:
.
I didn't do an exhaustive search but just tested most common non alphanumeric characters.
And just for the fun of it you can name an envar %_(){}[]$*+-\/"#',;.@!?%
:
C:\>set _(){}[]$*+-\/"#',;.@!?=xyz
C:\>echo %_(){}[]$*+-\/"#',;.@!?%
xyz
Character escaping in variable name
It is because the order of how Batch processes each command line. Simply put, the variable expansion is performed before analyzing special characters. That is why the carat is consumed by the variable expansion before being removed as an escape character. This is also why percent characters have to be escaped by themselves %%
instead of the standard carat ^
escape character.
Phase/order
1) Phase(Percent):
- A double
%%
is replaced by a single%
- Expansion of argument variables (
%1
,%2
, etc.)- Expansion of
%var%
, if var does not exists replace it with nothing- For a complete explanation read this from dbenham Same thread: percent expansion
1.5) Remove all
<CR>
(CarriageReturn 0x0d) from the line2) Phase(Special chars,
"
<LF>
^
&
|
<
>
(
)
: Look at each character
- If it is a quote (
"
) toggle the quote flag, if the quote flag is active, the following special characters are no longer special:^
&
|
<
>
(
)
.- If it is a caret (
^
) the next character has no special meaning, the caret itself is removed, if the caret is the last character of the line, the next line is appended, the first charater of the next line is always handled as escaped character.
<LF>
stops the parsing immediately, but not with a caret in front
For a full and great explanation (seriously bookmark this link!) see the answers here:
- How does the Windows Command Interpreter (CMD.EXE) parse scripts?
- Direct Answer: https://stackoverflow.com/a/4095133/891976
Escaping Characters in Python Variables
The proper way is to pass the arguments in a tuple to cursor.execute
:
cursor.execute("""INSERT INTO players (name, position, team, status) values (%s, %s, %s, %s)""", (name, position, team, status))
And let the cursor do the escaping.
How to escape variable name when using Roslyn C# Syntax Factory?
The implication from the API docs seems to be that it expects a valid C# identifier here, so Roslyn's not going to provide an escaping mechanism for you. Therefore, it falls to you to define a string transformation such that it achieves what you want.
The way to do this would be to look at how other things already do it. Look at HTML entities, which are always introduced using &
. They can always be distinguished easily, and there's a way to encode a literal &
as well so that you don't restrict your renderable character set. Or consider how C# strings allow you to include string delimiters and other special characters in the string through the use of \
.
You need to pick a character which is valid in C# identifiers to be your 'marker' for a sequence which represents one of the non-identifier characters you want to encode, and a way to allow that character to also be represented. Then make a mapping table for what comes after the marker for each of the encoded characters. If you want to do all of Unicode, the easiest way is probably to just use Unicode codepoint numbers. The resulting identifiers might not be very readable, but maybe that doesn't matter in your use case.
Once you have a suitable system worked out, it should be pretty straightforward to write a string transformation function which implements it.
escape characters in variables in a batch script
Your escaping seems to work on my end. Could it be you're using the percent sign %
for the constructed variable? Check this:
@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET CURRHOST=myServer
SET CURRCOMPANY=myCompany
SET CURRDB=string^^!string=string^^!string=string
ECHO correct: %CURRHOST%\%CURRCOMPANY%\!CURRDB!
ECHO broken : %CURRHOST%\%CURRCOMPANY%\%CURRDB%
SET NAME=%CURRHOST%\%CURRCOMPANY%\!CURRDB!
ECHO correct: !NAME!
ECHO broken : %NAME%
Outputs:
correct: myServer\myCompany\string!string=string!string=string
broken : myServer\myCompany\stringstring=string
correct: myServer\myCompany\string!string=string!string=string
broken : myServer\myCompany\stringstring=string
The broken strings exactly match the misbehaviour you describe.
Delayed expansion isn't exactly easy to get your head around and the official documentation is mostly bad. I'd recommend reading on SS64, here's their page on delayed expansion.
Related Topics
Cumulative Count of Unique Values in R
How Does One Merge Dataframes by Row Name Without Adding a "Row.Names" Column
Saving a List of Plots by Their Names()
How to Call the 'Function' Function
R How to Change One of the Level to Na
How to Show Corpus Text in R Tm Package
If_Else() 'False' Must Be Type Double, Not Integer - in R
Make R Studio Plots Only Show Up in New Window
Data.Table VS Plyr Regression Output
How to Retrieve the Most Repeated Value in a Column Present in a Data Frame
Ggplot2: How to Adjust Fill Colour in a Boxplot (And Change Legend Text)
How to Increase the Space Between Grouped Bars in Ggplot2
How to Pass Pandoc_Args to Yaml Header in Rmarkdown
How to Loop Over the Length of a Dataframe in R
R, Deep VS. Shallow Copies, Pass by Reference
How to Read Knitr/Rmd Cache in Interactive Session