Regex string in Go [duplicate] - regex

This question already has answers here:
"Unknown escape sequence" error in Go
(2 answers)
Closed 4 years ago.
I try to use string
"/{foo}/{bar:[a-zA-Z0-9=\-\/]+}.{vzz}"
in Go.
When I use ", I see error:
unknown escape sequence
When I use ', I get:
cannot use '\u0000' (type rune) as type string in array or slice literal
unknown escape sequence
How I can use this regular expression for MUX in my Go application?

When you mean \ character literally in string literals - it must be escaped additionally
"/{foo}/{bar:[a-zA-Z0-9=\\-\\/]+}.{vzz}"
otherwise you could use backticks instead of double quotes
`/{foo}/{bar:[a-zA-Z0-9=\-\/]+}.{vzz}`

According to Golang Language Specification.
string_lit = raw_string_lit | interpreted_string_lit .
raw_string_lit = "`" { unicode_char | newline } "`" .
interpreted_string_lit = `"` { unicode_value | byte_value } `"` .
So if you do not want to escape anything in your string literal, you need a raw one. and
The value of a raw string literal is the string composed of the uninterpreted (implicitly UTF-8-encoded) characters between the quotes
Golang does not use single quote ' as a string literal indicator. And your error with the double quote " is due to the compiler trying to escape \- and \/ as a part of the string before the regex interpreter.

Related

Remove parenthesis and Characters inside it [duplicate]

This question already has answers here:
Remove text between parentheses in dart/flutter
(2 answers)
Regular expresion for RegExp in Dart
(2 answers)
JavaScript/regex: Remove text between parentheses
(5 answers)
Closed 3 months ago.
I want to remove parenthesis along with all the characters inside it...
var str = B.Tech(CSE)2020;
print(str.replaceAll(new RegExp('/([()])/g'), '');
// output => B.Tech(CSE)2020
// output required => B.Tech 2020
I tried with bunch of Regex but nothing is working...
I am using Dart...
Using Dart, you don't have to use the forward slashes / to delimit the pattern. You can use a string and prepend it with r for a raw string and then you don't have to double escape the backslashes.
In your pattern you have to:
escape the parenthesis
negate the character class to match any character except the parenthesis
repeat the character class with a quantifier like * or else it will match a single character
The pattern will look like:
\([^()]*\)
Regex demo | Dart demo
Example
var str = "B.Tech(CSE)2020";
print(str.replaceAll(new RegExp(r'\([^()]*\)'), ' '));
Output
B.Tech 2020
Your Dart syntax is off, and seems to be confounded with JavaScript. Consider this version:
String str = "B.Tech(CSE)2020";
print(str.replaceAll(RegExp(r'\(.*?\)'), " ")); // B.Tech 2020

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] "(\\\\|\\^|\\$|\\.|\\||\\?|\\*|\\+|\\(|\\)|\\[|\\{)"

Detect \ using regex in R [duplicate]

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
>

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"

C++ Unrecognized escape sequence

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