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
Related
This question already has answers here:
Regex escape with \ or \\?
(5 answers)
Closed 2 years ago.
I tried to use regexes in C#
^(?=.*\d)(?=.*[a - z])(?=.*[A - Z])(?=.*[!##$%^*])(?=.*[a-zA-Z]).{6,20}$
but \d comes as an error if i put [0-9] instead it wont work as desired
This should check the string has a uppercase, lowercase, symbol and a number
You should use [0-9]. Probably it is more correct... \d will catch non-european digits like рен (it is a Devanagari digit).
For the reason:
you probably wrote:
var rx = new Regex("\d");
But in this way the \d is an escape sequence of the string instead of being a regex.
Write
var rx = new Regex(#"\d");
to deactivate the escape expansion of strings.
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.
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"
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.
This question already has answers here:
What is the difference between a regular string and a verbatim string?
(6 answers)
Closed 9 years ago.
From ReSharper, I know that
var v = #"something";
makes v something called a verbatim string. What is this and what is a common scenario to use it?
In a verbatim string, escape sequences (such as "\n" for newline) will be ignored. This helps you type strings containing backslashes.
The string is also allowed to extend over multiple lines, for example:
var s = #"
line1
line2";
The string will appear the same way you typed it in your source code, with line breaks, so you don't have to worry about indents, newlines etc.
To use quotes inside a verbatim literal, you just double them:
#"This is a string with ""quotes""."
It means that special chars don't need to be escaped, since you informed the compiler to expect special characters, and to ignore them. A common use case might be to specify a connection string:
string sqlServer = #"SERVER01\SQL";
This is perfectly valid, as opposed to in normal use where the backslash would be considered an escape character.