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

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.

Related

Regex: match string with parathese with '|' operator [duplicate]

This question already has answers here:
How do I make part of a regex match optional?
(2 answers)
Order of regular expression operator (..|.. ... ..|..)
(1 answer)
Closed 1 year ago.
I am trying to match a substring of "Number" or "Number(s)" in a string using a single Regex. However, I can get them to match individually, but not together.
Individually,
'Number' can match the word number
'Number[(]s[)]' can match Number(s).
However, if I put them together and do "Number|Number[(]s[)]" it is not matching for (s) of "Number(s)".
What I have tried:
1: Put \b boundary around the second string, doesn't work.
2: Use \ to escape, but C# yells at me for unrecognized escape sequence, so I opted out of this option
I know that I can use two regex to do what I want, but I wanted to understand what is wrong here and learn.
Number|Number[(]s[)] wont match Number(s) because it's first part "Number" matches it.
Try change the pattern part order: Number[(]s[)]|Number. This will try to match first with the string with parentheses and if it can't it will try the short form.
Also the pattern should be: Number\(s\)|Number
The unrecognized escape error message comes because if you want this pattern written as a string literal you must escape the backslash signs: "Number\\(s\\)|Number".

regex in C# not working properly for numeric check [duplicate]

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.

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.

Regex to only accept Numeric and Decimal Value not any special or character in c# [duplicate]

This question already has answers here:
Regular expression to match numbers with or without commas and decimals in text
(11 answers)
Closed 5 years ago.
How do I restrict special characters and character in a Textbox?
I'm using this code but I am not restricting special characters and character
code :-
if (!Regex.IsMatch(((Windows.UI.Xaml.Controls.TextBox)sender).Text, #"^\\d*\\.?\\d*$"))
{
// Write Code
}
if you're using # in front of string, you don't have to escape characters. So, remove all those extra backslash chars and your regex should work. Like this:
if (!Regex.IsMatch(((Windows.UI.Xaml.Controls.TextBox)sender).Text, #"^\d*\.?\d*$"))
EDIT:
use sites like this to test your regex.

Categories