I want to create a string that contains all possible special chars.
However, the compiler gives me the warning "Unrecognized escape sequence" in this line:
wstring s=L".,;*:-_⁊#‘⁂‡…–«»¤¤¡=„+-¶~´:№\¯/?‽!¡-¢–”¥—†¿»¤{}«[-]()·^°$§%&«|⸗<´>²³£€™℗#©®~µ´`'" + wstring(1,34);
Can anybody please tell me which one of the characters I may not add to this string the way I did?
You have to escape \ as \\, otherwise \¯ will be interpreted as an (invalid) escape sequence:
wstring s=L".,;*:-_⁊#‘⁂‡…–«»¤¤¡=„+-¶~´:№\\¯/?‽!¡-¢–”¥—†¿»¤{}«[-]()·^°$§%&«|⸗<´>²³£€™℗#©®~µ´`'" + wstring(1,34);
Escape sequence is a character string that has a different meaning than the literal characters themselves. In C and C++ the sequence begins with \ so if your string contains a double quote or backslash it must be escaped properly using \" and \\
In long copy-pasted strings it may be difficult to spot those characters and it's also less maintainable in the future so it's recommended to use raw string literals with the prefix R so you don't need any escapes at all
wstring s = LR"(.,;*:-_⁊#‘⁂‡…–«»¤¤¡=„+-¶~´:№\¯/?‽!¡-¢–”¥—†¿»¤{}«[-]()·^°$§%&«|⸗<´>²³£€™℗#©®~µ´`')"
+ wstring(1,34);
A special delimiter string may be inserted outside the parentheses like this LR"delim(special string)delim" in case your raw string contains a )" sequence
Related
I am trying to send two commands at once with sprintf. Commands should be separated with 0x0A (LF). I thought I could enter special characters using two slashes, so I am writing:
sprintf(tmpstr,"VSET1:%ld.%3.3d\\x0AVSET2:%ld.%3.3d",mv/1000, AbsVal((int)mv%1000), mv / 1000, AbsVal((int)mv % 1000));
and it seems only the second command (VSET2) is recognized.
What am I doing wrong?
Use \n in the format string. Also, use a single backslash not \\.
If you are writing your buffer to a file, open the file in binary mode.
Whether you use \n or \x0A, you have to open the file in binary mode to avoid non-portable translations.
See Escape sequences.
When you use \\x0A in a string literal, the first backslash escapes the second backslash. As a result, the string contains a backslash character, '\\', followed by characters 'x', '0', and 'A'.
To use the character represented by 0x0A, you need to use \x0A.
You should be using a single backslash instead of two backslashes. Try the statement given below:
sprintf(tmpstr,"VSET1:%ld.%3.3d\x0AVSET2:%ld.%3.3d",mv/1000, AbsVal((int)mv%1000), mv / 1000, AbsVal((int)mv % 1000));
However, what you have done in your program will print a string "\x0A", rather than an ASCII character (0xAA (Line Feed)).
In C, all escape sequences consist of two or more characters, the first of which is the backslash, \ (called the "Escape character"); the remaining characters determine the interpretation of the escape sequence.
C deal with backslashes as escape sequences by default. However, in your program, you have told C compiler to not use your backslash as an escape sequence by adding an extra backslash to your string.
This works perfect. You not only get to insert \n but looks correct in the code. No need for a \ at the end of lines either. I use this for big paragraphs. Personal data has been obfuscated.
enter code here
wchar_t msg[200];
swprintf(msg, L"XYZ%d: ABCD Limit set to %d%%. %d times it has abcd and xyz rstu %d%%\n"
"Do you want to fix it?\n"
"An Yes will fix it\n"
"No will ignore it and continue\n"
"Cancel will abort the run\n", xxx, yyy, zzz, aaa);
I have a line in my file which contains the following string
$print = "SM_sdo_debugss_cxct6_CSCTM_4 \csctm_gen[4]_ctm_i_nctm_I_csctm (4+5)";
$my_meta = '\csctm_gen[4]_ctm_i_nctm_I_csctm';
print "I got this\n" if($print =~ /\Q$my_meta\E/);
But it's not able to find the $my_meta string in $print. Why?
Your first string is in double quotes, so backslash escape sequences are processed.
\cs stands for Ctrl-S, which can also be written chr(19) or "\x13".
Your second string is in single quotes, which ignores backslash escapes (apart from \\ and \').
So your regex ends up looking for a 3-character sequence \ c s, but your target string contains a single byte 0x13.
To fix this, either write "... \\cs ..." in your first string (the first backslash escapes the second one), or use single quotes for your first string ('... \cs ...').
I have a text file full of strings (computer paths) which I want to process by replacing every backslash with an underscore, in addition to replacing every number ( integer or float) with an underscore as well, the original string looks like that :
string = "\Software\Microsoft\0\Windows\CurrentVersion\Internet Settings\5.0\Cache"
Usually, I could replace easily the backslash with the following command:
string=string.replace('\\','_')
and apply some regular expressions such as: '(\d(?:\.\d)?)' to replace the numbers.
However in my case I couldn't do either, because python recognise always '\0' as a null character and '\5.0' as ENQ, in fact any number follow the backslash will be treated the same way as well.
Any suggested way to replace them ?
e.g. is there a way to convert my string to raw string as a start ?
Always remember: Backslash(\) escapes special characters. If you want to use the backslash itself, you need to escape it too. Your string should look like this:
string = "\\Software\\Microsoft\\0\\Windows\\CurrentVersion\\Internet Settings\\5.0\\Cache"
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 see this:
\str
\\str
If I want to get a string containing 5 backslashes, should I write 10 backslashes, like this?
x = "\\\\\\\\\\str"
[...] If I want to get a string containing 5 \ ,should i write 10 \ [...]
Yes, you should. To write a single \ in a string, you write it as "\\".
This is because the \ is a special character, reserved to escape the character that follows it. (Perhaps you recognize \n as newline.) It's also useful if you want to write a string containing a single ". You write it as "\"".
The reason why \\\str is invalid, is because it's interpreted as \\ (which corresponds to a single \) followed by \s, which is not valid, since "escaped s" has no meaning.
Have a read of this section about character vectors.
In essence, it says that when you enter character string literals you enclose them in a pair of quotes (" or '). Inside those quotes, you can create special characters using \ as an escape character.
For example, \n denotes new line or \" can be used to enter a " without R thinking it's the end of the string. Since \ is an escape character, you need a way to enter an actual . This is done by using \\. Escaping the escape!
Note that the doubling of backslashes is because you are entering the string at the command line and the string is first parsed by the R parser. You can enter strings in different ways, some of which don't need the doubling. For example:
> tmp <- scan(what='')
1: \\\\\str
2:
Read 1 item
> print(tmp)
[1] "\\\\\\\\\\str"
> cat(tmp, '\n')
\\\\\str
>
The two characters )" terminate the raw string literal in the example below.
The sequence )" could appear in my text at some point, and I want the string to continue even if this sequence is found within it.
R"(
Some Text)"
)"; // ^^
How can I include the sequence )" within the string literal without terminating it?
Raw string literals let you specify an almost arbitrary* delimiter:
//choose ### as the delimiter so only )###" ends the string
R"###(
Some Text)"
)###";
*The exact rules are: "any member of the basic source character set except:
space, the left parenthesis (, the right parenthesis ), the backslash \,
and the control characters representing horizontal tab,
vertical tab, form feed, and newline" (N3936 §2.14.5 [lex.string] grammar) and "at most 16 characters" (§2.14.5/2)
Escaping won't help you since this is a raw literal, but the syntax is designed to allow clear demarcation of start and end, by introducing a little arbitrary phrase like aha.
R"aha(
Some Text)"
)aha";
By the way note the order of ) and " at the end, opposite of your example.
Regarding the formal, at first sight (studying the standard) it might seem as if escaping works the same in raw string literals as in ordinary literals. Except one knows that it doesn't, so how is that possible, when no exception is noted in the rules? Well, when raw string literals were introduced in C++11 it was by way of introducing an extra undoing translation phase, undoing the effect of e.g. escaping!, to wit, …
C++11 §2.5/3
” Between the
initial and final double quote characters of the raw string, any transformations performed in phases 1
and 2 (trigraphs, universal-character-names, and line splicing) are reverted; this reversion shall apply
before any d-char, r-char, or delimiting parenthesis is identified.
This takes care of Unicode character specifications (the universal-character-names like \u0042), which although they look and act like escapes are formally, in C++, not escape sequences.
The true formal escapes are handled, or rather, not handled!, by using a custom grammar rule for the content of a raw string literal. Namely that in C++ §2.14.5 the raw-string grammar entity is defined as
" d-char-sequenceopt ( r-char-sequenceopt ) d-char-sequenceopt "
where an r-char-sequence is defined as a sequence of r-char, each of which is
” any member of the source character set, except
a right parenthesis ) followed by the initial d-char-sequence
[like aha above] (which may be empty) followed by a double quote "
Essentially the above means that not only can you not use escapes directly in raw strings (which is much of the point, it's positive, not negative), you can't use Unicode character specifications directly either.
Here's how to do it indirectly:
#include <iostream>
using namespace std;
auto main() -> int
{
cout << "Ordinary string with a '\u0042' character.\n";
cout << R"(Raw string without a '\u0042' character, and no \n either.)" "\n";
cout << R"(Raw string without a '\u0042' character, i.e. no ')" "\u0042" R"(' character.)" "\n";
}
Output:
Ordinary string with a 'B' character.
Raw string without a '\u0042' character, and no \n either.
Raw string without a '\u0042' character, i.e. no 'B' character.
You can use,
R"aaa(
Some Text)"
)aaa";
Here aaa will be your string delimiter.