to use escape character rightly - r

i found it is difficult to create character : " is entered as \ "
when i input in R:
" is entered as \"
+
" is entered as \ "
[1] " is entered as "
>" is entered as \\ "
[1] " is entered as \ "
>" is entered as \\"
[1] " is entered as \"
how can i get character " is entered as \ "?
i am still confused
cat("is entered as \" )
is entered as >
> "is entered as \\"
[1] "is entered as \"
> print ("is entered as \\")
[1] "is entered as \"

"hoge \\" is actually hoge \
print shows \ as \\, so you find \\ for \.
try cat:
> cat("is entered as \\" )
is entered as \
and probably nchar will manifest this:
> nchar("\\" )
[1] 1

Is this what you were trying to achieve?:
> x <- "START \" is entered as \\\" END"
> cat(x)
Gives:
START " is entered as \" END
You have to escape both the double quote ", and the backslash \, in order to have them display normally.
To clear up any confusion between whether the double quotes printed in the output are part of the string, or just symbols that wrap around the string, I've added START at the start of the string and END at the end.

Related

R: dealing with " " symbols

I am using the R programming language. I am copying text data from a website that contains many quotation marks, i.e. "" . When I try to create a data frame that contains this text, I will get an error because of conflicting "" symbols.
For example:
a <- " "blah" blah blah"
Error: unexpected symbol in "a <- " "blah"
Normally, I would have tried to use the gsub() function to remove these quotation marks from the data frame, but I can not even create the data frame to begin with. Of course, I could bring this text into a word processing software and click " ctrl + H" to replace all quotation marks ("") with an empty space (). But is there a way to do this in R itself?
Thanks
The typical way you would handle this would be to escape the literal double quotes with backslash:
a <- " \"blah\" blah blah"
[1] " \"blah\" blah blah"
You could also wrap your string literal inside single quotes and then not even have to escape the double quotes:
a <- ' \"blah\" blah blah'
[1] " \"blah\" blah blah"

Remove double quotes from text in r

I want to eliminate double quotes from text in R. Is there a better way to do it?
I tried below code but it's still not removing double quotes:
gsub("\"", "", a$answer)
The problem with what you tried is that you want the regular expression (i.e. pattern) to be \", but backslashes are special to R, so you need to write it twice in R so it ends up as a single backslash in the pattern.
For example,
withquotes <- ' this is a double quote: " '
gsub('\\"', "gone!", withquotes)
# [1] " this is a double quote: gone! "
We can also do this without escaping the double quotes
gsub('"', "gone!", withquotes)
#[1] " this is a double quote: gone! "
data
withquotes <- ' this is a double quote: " '

Usage of & in xquery concat

How can I use & (the ampersand character) in an XQuery concat statement? I'm using eXist DB and this works:
concat("Marvin ", "and", " Peter")
but this doesn't:
concat("Marvin ", "&", " Peter")
I'm getting the error: expecting '"', found '&'
Escaping the ampersand with a \ doesn't work.
Since & is the escape character for XML character and entity references, it cannot be used as a literal character in either XML or XQuery strings. You have to use an entity to encode it. You can either use the predefined &, or reference it via its Unicode codepoint with &#[...]; (decimal) or &#x[...]; (hexadecimal):
concat("Marvin ", "&", " Peter"),
concat("Marvin ", "&", " Peter"),
concat("Marvin ", "&", " Peter")

How to exclude character " in a token JavaCC

Hello i´m working with JavaCC and I am writing a token that put one String between " ". Context:
void literalString(): {} { """ (characteresString())? """ }
void characteresString(): {} { <characterString> | characteresString() <characterString> }
So i made this token to put one String:
TOKEN : {<characterString : ~["\", "] >}
The problem is I don´t know how to exclude the " symbol in the token, if I put """ it gives me error, if i put one " error again.
Thank you in advance
Instead of
void literalString(): {} { """ (characteresString())? """ }
use a token definition
TOKEN : { <STRING : "\"" (<CHAR>)* "\"" >
| <#CHAR : ~["\""] > // Any character that is not "
}
Now this defines a string to be a ", followed by zero or more characters that are not "s, followed by another ".
However some languages have further restrictions, such as only allowing characters in a certain range. For example if only printable ascii characters excluding "s where allowed, then you would use
TOKEN : { <STRING : "\"" (<CHAR>)* "\"" >
| <#CHAR: [" ","!","#"-"~"]> // Printable ASCII characters excluding "
}
But, say you want to allow " characters if the are preceded a by \ and you want to ban \ characters unless they are followed by a " or another \ or an n. Then you could use
TOKEN : { <STRING : "\"" (<CHAR> | <ESCAPESEQ>)* "\"" >
| <#CHAR: [" ","!","#"-"[","]"-"~"] > // Printable ASCII characters excluding \ and "
| <#ESCAPESEQ: "\\" ["\"","\\","n"] > // 2-character sequences \\, \", and \n
}

Escape character for " in ValidationExpression in ASP.NET

I am using regular expression to filter the invalid input entered by the end user.
The acceptable input is word, space, digital and . / # , # & $ _ : ? ' % ! – ~ " | + ; ” { } - \.
Below is my code.
<asp:RegularExpressionValidator ID="rgVEditTB1" runat="server" ControlToValidate="txtEditTB1"
ValidationExpression="^[\w\s\d\-\.\/\#\,\#\&\$\:\?\"\'\%\!\–\~\|\+\;\”\{\}\-\\]+$" ErrorMessage="Invalid Special Character" />
However, I am encountering problem to escape " in the ValidataionExpression, it errors out with
Server Tag is not well formed error.
I tried to change the escape character to:
\""
\"
""
It also gives me the same error.
What should be the correct escape character to put in the ValidationExpression?
You should be able to pass in the HTML encoding values. So, passing " would be like passing ". Something like this: ValidationExpression="^[^"]+$". In this regex I am saying: Match any character from the beginning till the end of the string which is not a quotation mark (").
The same applies to the other special symbols. You can take a look here for more encoding values.

Resources