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

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.

Related

How to get all possible Regex Matches [duplicate]

This question already has answers here:
C# Code to generate strings that match a regex [closed]
(4 answers)
Closed 3 years ago.
Based off a regex string I would like to get a list of all the possible strings that would match the regex.
Example:
Given a regex string like...
^(en/|)resources/case(-| )studies/
I want to get a list of all the possible strings that would match the regex expression. Like...
^en/resources/case-studies/
or
^/resources/case-studies/
or
^en/resources/case studies/
or
^/resources/case studies/
Thank you
Note that in regex ^ denotes the beginning of the line. You must escape it
Try
\^(en)?/resources/case(-|\s)studies/
explanation:
\^ is ^ escaped.
(en)? is optionally en, where ? means zero or one times.
/resources/case the text as is.
(-|\s) minus sign or white space.
studies/ the text as is.
See: https://dotnetfiddle.net/PO4wKV

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.

C# - Regular expression [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
I'm studying a program written in c #.
I do not understand this regular expression, can you explain to me what it does?
Thank you
Match numberMatch = Regex.Match(patternOutput, "(#+)([\\.|,])(#+)");
https://regex101.com/ provides a convenient explanation to pretty much all possible kinds of regex syntaxes. In your case the regex matches a literal # character at the beginning, followed by one of the characters in the square brackets (each of them literally, where the backslash is escaped by another one): [\\.|,], so this will match either \, ., |, ,. And at the end follows one more # character.

Regular expression accepts partial match [duplicate]

This question already has answers here:
C# Regex to match the word with dot
(3 answers)
Regex plus vs star difference? [duplicate]
(9 answers)
Closed 3 years ago.
I have some validation code that checks a string against a regular expression.
Regex regex = new Regex(RegexPattern);
if (!regex.IsMatch(value))
{
errorMessage = "The data is not in the correct format.";
return false;
}
If I set the regular expression pattern to ^[0-9]*.[0-9]*.[0-9]*.[0-9]*$, it correctly accepts 1.0.0.0; however, it also accepts 1.0.0..
How can I modify the pattern so that 1.0.0.0 is accepted but 1.0.0. is rejected?
[0-9]* means 0 or more occurrence of [0-9]
[0-9]+ means 1 or more occurrence of [0-9]
^[0-9]*.[0-9]*.[0-9]*.[0-9]*$
Change * to +:
^[0-9]+.[0-9]+.[0-9]+.[0-9]+$
Just a slight misunderstanding about * and +. The former accepts either no occurrences or more (>=0), the latter only matches if such vocabulary occurs at least once (>=1).
^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$
I usually also escape the dot for safety, not entirely sure if it's necessary but I still do it. :)
You can use this pattern:
^\d+\.\d+\.\d+\.\d+$
Explanation:
^ - begin of string
\d - any digit
+ - at least one char
\. - exactly a dot char
$ - end of string
By the way, your input looks like an IP address. If so you can modify your reges like this:
^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$
{1,3} means: 'from 1 to 3 chars'
Demo: regex101
To match 4 sets of digits separated by a period
^\d+(?:\.\d+){3}$

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