gsub("BLAH", "", "BLAH\WHAT") won't let x have a backslash? [duplicate] - regex

This question already has answers here:
How to escape a backslash in R? [duplicate]
(2 answers)
Closed 8 years ago.
I'm doing some batch string clean up and a lot of the entries look like this:
"ABC\Company Co."
Which causes weird errors, and I can't seem to remove the backslash.
For example, try entering this into your console:
gsub("BLAH", "", "BLAH\WHAT")
and you get:
Error: '\W' is an unrecognized escape in character string starting ""BLAH\W"
I know that it's thinking \W is a command.. I'm actually suprised that gsub's 'interpreting' x, since x is just the string I want to sub out. I don't get why gsub cares what's actually in x, just that it should replace "BLAH" with "" within "BLAH\WHAT"...
The obvious solution would be to remove the \ from the string ahead of time.
gsub("\\", "", "BLAH\WHAT")
But then you get the exact same error message!
Thoughts? Thanks!

Use
gsub("\\\\", "", "BLAH\\WHAT")
which gives
[1] "BLAHWHAT"
To produce one backslash, you need to escape it using a \. Thus, "\\\\" produces two backslashes, which matches the two inside "BLAH\\WHAT".
See these related questions:
How to escape a backslash in R?
How to escape backslashes in R string

Related

Is there a way to print a string as a Literal? [duplicate]

This question already has answers here:
Escaping a C++ string
(5 answers)
Closed 4 years ago.
As you know we can use something like this:
string s = L("some\nstr\t");
My question is if there is a way to print a string using a literal. For example something like this:
string s = "s\nsome\n"
cout<< L(s); // the output printed should be s\nsome\n and not new lines
Thank you.
Yes,
string s = "Here is \\nan \\n example"
cout<< s;
Francois' answer is already good, but I'd like to elaborate a little more about what's happening here...
When you put \n in a string, that is a single character. The \ is saying "don't treat whatever comes next as you normally would, escape it." An escaped n is a newline, so \n is the newline character.
So if you want \ in your string, how would you get it? Normally this is treated as the escape character, but we want it to act as just a regular character. So how do we do that? We escape it! \\ will be interpreted as a single \ character.
So if you want s\nsome\n to be printed, you need to construct your string as "s\\nsome\\n". Notice that the n's aren't being escaped, the escape characters are!

How to replace '\' with '/' using regex in Python [duplicate]

This question already has answers here:
How to convert back-slashes to forward-slashes?
(9 answers)
Closed 4 years ago.
I tried this but did not work.
import re
s = "Ex\am\ple String"
replaced = re.sub("\", "/", s)
print replaced
Any suggestions?
Just use this code:
s = s.replace("\\", "/")
It replaces all the backslashes with the forward slash. The reason for the two backslashes is because you have to escape the other backslash.
Try it like this with \\\\:
import re
s = "Ex\am\ple String"
replaced = re.sub("\\\\", "/", s)
print replaced
You likely aren't using enough backslashes.
Backslash is often an escape character. Try commenting here using one \, then two \, you'll notice you only get one in the output here.
code version of what I just typed to show what I mean:
Backslash is often an escape character. Try commenting here using one \,
then two \\, you'll notice you only get one in the output here.
I wrote a similar code in R for when I cut and paste file locations
fileLoc = function(){
Rlocation = gsub( "\\\\","/",readClipboard())
return(Rlocation)
}
Notice it required 4 backslashes.
Try the following:
s = "Ex\am\ple String"
replaced = re.sub("\\\\", "/", s)
You have two problems: First, your string is not what you think it is, because '\a' is interpreted as an escape literal. To fix this, use a raw or regex string,
s = r"Ex\am\ple String"
Now you can do what you want using
replaced = re.sub(r"\\", "/", s)
Here, two \ are needed because re expect a single \ to be an escape character. Also, the r"\\" needs to be a raw string because Python more generally also interprets "\" as an escape character. To "escape the escape character" itself, you can do the double \ trick again, and so another way of doing it would be
replaced = re.sub("\\\\", "/", s)
Better method
Escaping and the re module aside, for simple replacements like this, you should use the str replace method,
replaced = s.replace("\\", "/")
("only" two \ needed because we are only using "bare" Python, not also re)

RegEx escape function in R [duplicate]

This question already has answers here:
Is there an R function to escape a string for regex characters
(5 answers)
Closed last year.
In a R script, I'd need to create a RegEx that contains strings that may have special characters. So, I should first escape those strings and then use them in the RegEx object.
pattern <- regex(paste('\\W', str, '\\W', sep = ''));
In this example, str should be fixed. So, I'd need a function that returns escaped form of its input. For example 'c++' -> 'c\\+\\+'
I think you have to escape only 12 character, so a conditional regular expression including those should do the trick -- for example:
> gsub('(\\\\^|\\$|\\.|\\||\\?|\\*|\\+|\\(|\\)|\\[|\\{)', '\\\\\\1', 'C++')
[1] "C\\+\\+"
Or you could build that regular expression from the list of special chars if you do not like the plethora of manual backslashes above -- such as:
> paste0('(', paste0('\\', strsplit('\\^$.|?*+()[{', '')[[1]], collapse = '|'), ')')
[1] "(\\\\|\\^|\\$|\\.|\\||\\?|\\*|\\+|\\(|\\)|\\[|\\{)"

Print on the screen the symbol \ as text [duplicate]

This question already has answers here:
using \ in a string as literal instead of an escape
(2 answers)
Closed 7 years ago.
I want the following printout to appear in the my screen when I run my code:
\begin{tabular}
\hline \\
For that, I am using the following command on my code:
std::cout<<"\begin{tabular}<< std::endl;
std::cout<<"\hline \\"<< std::endl;
And I'm getting the following compiler message (regarding the second line of code):
unknown escape sequence: '\h'
and the following incomplete printout:
egin{tabular}
hline\
Where in the first one the "\b" is missing and the first and last \ are missing for the second sentence.
The question is: does anyone know how I can print the \ symbol as text, such that it will get printed and not be interpreted as a command, etc?
The backslash forms escape sequences in C++. You have two options:
Double all the backslashes, a la "\\hline \\\\" (the backslash will escape itself, just as in TeX).
Use C++11 raw strings, which look like R"(\hline \\)" (the text is enclosed by the parens inside the quotes).
Just escape it wiht '\'. So if you want to print out '\' character you must do:
cout<<'\\'<<endl;
Use double backslash (\) if you would like to print \ as a character in your output, otherwise, single \ followed by some character has inherent meaning of some special character, e.g. \n for newline, \r for carriage return \t for tab etc
Double all the backslashes.
e.g.
std::cout<<"\\hline \\\\"<< std::endl;
backslash is an escape code in C++.
As in, below, the "\"escape sequence and n is escape code. Which means a newline character.
"hello\n"
so if you want to print \ as well, you need to escape it too.
"hello\\hi"

Escaping backslash using sub [duplicate]

This question already has answers here:
How do I deal with special characters like \^$.?*|+()[{ in my regex?
(2 answers)
Closed 8 years ago.
I have a string template, read from a file, with the content:
template <- "\\begin{tabular}\n[results]\n\\end{tabular}"
I want to replace results with some text I have generated in my code, to compose a laTeX table, but when I run:
sub("\\[results\\]","text... \\hline text...",template)
I have the following output:
"\\begin{tabular}\ntext... hline text...\n\\end{tabular}"
The backslash is not escaped and I don't understand why, because I'm using \\ for this purpose.
I am using R-3.0.2.
The regex engine is consuming the \\ for a potential capture group, you need to add two more backslashes:
sub("\\[results\\]","text... \\\\hline text...",template)
[1] "\\begin{tabular}\ntext... \\hline text...\n\\end{tabular}"