The \ Symbol usage in C# strings [duplicate] - c#

This question already has answers here:
Escaping backslashes in string
(2 answers)
Closed 6 years ago.
I have a line of code like this
process.StartInfo.WorkingDirectory =Directory.GetCurrentDirectory() + "\bin";
but in the second string the "\" symbol isn't seen like a character of the string, how i can fix it?
The outputs of this line is "C:\Users\User\Desktop\Tesin" where Directory.GetCurrentDirectory()= "C:\Users\User\Desktop\Test"

Use #"\bin" or you need to escape any \ to \\ like "\\bin"
be aware using #"blahblahblah" changes the behave of escaping (a " must be escaped with "" instead of \" )

write "\\bin" - the \ character needs to be escaped with \ so - \

In many languages, the backslash (\) is an escape character, meaning it tells the system to take the following character literal.
You can just double the backslash, and it will work: "\\bin"

Related

How can I print \" In C# [duplicate]

This question already has answers here:
How do I write a backslash (\) in a string?
(6 answers)
Closed 2 years ago.
I'm trying to print \"Hey\" in the Console But when I run it It just print the "Hey" without \ and when i put another \ in the beginning and \ in end it says theres an Error
You have to escape the " as well as the \. Both should be escaped with a backslash itself. Your final result should look something like:
Console.WriteLine("\\\"Hey\\\"");

Why is my regex not escaping this backslash? [duplicate]

This question already has answers here:
C# Regex Issue "unrecognized escape sequence"
(3 answers)
Regular expression to allow backslash in C#
(3 answers)
Closed 3 years ago.
I am looking to create a list of file inside a folder, all matching a regex.
But the regex crash whenever I put a path containing backslash inside the regex.
string test = #"C:\TEMP"; // or "C:\\TEMP"
Regex reg = new Regex(test + "\\" + "tstNL02" + #"_(.*).csv"); // it crash here
FileList = Directory.GetFiles(test).Where(path => reg.IsMatch(path)).ToList();
ArgumentException: parsing "C:\TEMP\tstNL02_(.*).csv" - Unrecognized escape sequence \T.
As far as I know, using a # or escaping the backslash should prevent the regex from interpreting backslashes in a string as an escaping character (and if I remove test from the regex but leave the \\, regex doesn't crash).
If I put #"C:\\TEMP" the Regex doesn't parse any and the match fail C:\\TEMP\tstNL02_(.*).csv
I fixed my problem by going another way but I was wondering why and how to fix this backslash-in-a-variable thing ?
Edit: problem didn't came from regex but from the fact I was using the same string for regex and Directory.GetFiles. Adding escaping backslashes to the string so Regex worked correctly would cause Directory.GetFiles to not escape those added backslashes, thus not matching files

How to add {,},[,] brackets to regex pattern to allow them as special characters [duplicate]

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
I am having this below regex pattern. I want to allow these below characters as special characters.
{
}
[
]
Regex regEx = new Regex(#"^(?=.*[A-Z])(?=.*[!##$%^&*()+_<>~-])(?=.*[0-9])(?=.*[a-z]).{8,15}$");
Is there a way to add them
You'll need to escape them within the regex. Within a regular expression, you can refer to "\[" to get a '[' character, for example.
Note that in C, C#, C++, Java, etc, you usually need to double up the '\' character to escape the escape character. So when adding '[' to your regex, you would actually use "\\[". In your case you're using an #"" so you've escaped that trap.

How to properly escape \ character in c#? [duplicate]

This question already has an answer here:
Backslash Escape Sequence displaying both backslashes
(1 answer)
Closed 7 years ago.
I have a string like,
var log = "C:\\folder\\folder2\\this.txt";
For some reason this does not escaped the backslash, it still shows the backslashes in the string as below:
C:\\folder\\folder2\\this.txt
How do I properly escaped the \ character to allow writing to a file at such a location?
You are looking at it in the debugger, There is no double back slash in actual string. The debugger shows it like that.
Do:
Console.WriteLine(log);
and it will display the string without double back slashes in the console.
You are bound to see double backslashes if you're in the IDE, but they won't be there when you actually use the string.
As a bonus, you can rewrite your string as var log = #"C:\folder\folder2\this.txt";

C# .NET Regex 'Unrecognized escape sequence' [duplicate]

This question already has answers here:
Unrecognized escape sequence for path string containing backslashes
(5 answers)
Closed 7 years ago.
I'm trying to validate a password field using regex under the namespace System.Text.RegularExpressions but I'm getting three errors for
'Unrecognized escape sequence'.
When I double click on the errors it highlights the '-' in my expression for the character range but I don't know why this is wrong.
// password must contain one uppercase, one lowercase and one digit
Regex reg = new Regex("^(?=.*[!##$%^&*()\-_=+`~\[\]{}?|])(?=.+[a-z])(?=.+[A-Z])(? =.+[0-9]).{8,50}$");
Just add an # before the first quote to make it a verbatim string literal or escape the backslashes as \\.
it seems you have one space after ?
(? =.+[0-9]).{8,50}
remove that.

Categories