Regular Expression for data annotations [duplicate] - c#

This question already has answers here:
Strong password regex [duplicate]
(4 answers)
Closed 8 years ago.
How do I write a regular expression for the following
at least 6 characters
at least 1 uppercase
at least 1 lowercase
at least 1 number
at least 1 special character ("#¤%&/( æøå etc.)
I tried the below regex but it isn't working.
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[#+-?$!]).{8,}$

Below regex would satisfy all of your conditions,
^(?=.{6,})(?=.*?[A-Z])(?=.*?[a-z])(?=.*?\d)(?=.*?\W).*$
DEMO
(?=.{6,}) at least 6 characters
(?=.*?[A-Z]) at least 1 uppercase
(?=.*?[a-z]) at least 1 lowercase
(?=.*?\d) at least 1 number
(?=.*?\W) at least 1 non-word character

Related

Need Regular expression for the following [duplicate]

This question already has answers here:
Regular expression to validate numbers separated by commas or dashes [closed]
(2 answers)
Closed 2 years ago.
I have to write the Regex expression which accepts - and only numbers either single four digit number or two 4 digit numbers seperated by hyphen as shown below
2751, 2759-2764, 2766-2774, 2776-2777, 2890-2897
3945-3974, 3979, 3984-3999
I have used this Regex ^[0-9_,]+ but this line Regex.IsMatch(line, #"^[0-9_,]+$") returns false.
Regards,
Nagasree
The pattern that you tried is not matching as there is no hyphen or space in the character class. But when you would add those, the pattern still does not take any format into account.
You could match 4 digits with optional hyphen and 4 digits part. Then repeat that preceded by a space:
^[0-9]{4}(?:-[0-9]{4})?(?:, [0-9]{4}(?:-[0-9]{4})?)*$
Regex demo
var s = "2751, 2759-2764, 2766-2774, 2776-2777, 2890-2897";
Console.WriteLine(Regex.IsMatch(s, #"^[0-9]{4}(?:-[0-9]{4})?(?:, [0-9]{4}(?:-[0-9]{4})?)*$"));
Output
True

Regular expression for US phone number [duplicate]

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
US Phone Number Verification
(13 answers)
Regex to match all us phone number formats
(10 answers)
Regular expression to match US phone numbers
(5 answers)
Closed 3 years ago.
I am new to regex and I am burning hours just to get it right. I need to allow these patterns
NPA-XXX-XXXX
(NPA) XXX-XXXX
NPAXXXXXXX
where NPA = numbers 0 to 9
and X is also any number 0 to 9
so this is valid
123-456-7890
1234567890
(123) 456-7890
but not this
(123)-456-7890 // because there is a dash after closing parenthesis
(123)456-7890 // because there is no space after closing parenthesis
QWE-456-7890 // because there are one or more alpha characters
I use this
Regex r = new Regex(#"^?\(?\d{3}?\)??-??\(?\d{3}?\)??_??\(?\d{4}?\)??-?$");
from System.Text.RegularExpressions
What would be the regular expression that would match the valid?
One way to look at this:
all numbers end in XXX-XXXX, with the - being optional, so that's d{3}\-?\d{4}
numbers start with either (NPA) or NPA-, with the - being optional, so that's \(\d{3}\) (ending in a space) or \d{3}
Since you can 'or' parts of a regex together with (a|b), the whole regex becomes:
(\(\d{3}\) |\d{3}\-?)\d{3}\-?\d{4}
Note that \( escapes the parenthesis, so it's not seen as part of the regex language, but an actual character you want to match.

C# Regex, can i set MaxValue instead of MaxLength on my string? [duplicate]

This question already has answers here:
How do I match an entire string with a regex?
(8 answers)
Closed 3 years ago.
I am trying to make a textbox in UWP only allow the following input: 9 digits, followed by a '.' (optional), followed by 2 digits (optional), by using Regex.
So far i have come up with an expression that allows 9 digits, or 9 digits and a '.'
string pattern = #"^(?<Number>([0-9]{0,9})(\.?)([0-9]{0,2}?))";
I want it to accept 123456789 or 123456789.12 as inputs.
But now it only accepts 123456789 or 123456789.
If you want to match 2 optional digits you could make the group optional (?:[0-9]{2})? because [0-9]{0,2} Matches 0, 1 or 2 digits.
The same goes for {0,9} which matches from 0 - 9 times a digit.
To match the whole pattern you should add an anchor $ to assert the end of the string.
If you don't need the capturing groups you could update your pattern to:
^(?<Number>[0-9]{9}\.?(?:[0-9]{2})?)$
Regex demo

RegEx to match only {n} digits and not much or less surrounded by whatever [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 3 years ago.
I'm trying to match only {n} digits not more or less and may be surrounded by characters or special symbols
example:
suppose the {n} = {14}
*12345678901234*300 OK
12345678901234x21 OK
*123456789012345*300 NOT OK
12345678901234 OK
123456789012345 NOT OK
You could use negative lookarounds to assert what is directly on the left and right is not a digit and match 14 digits:
(?<!\d)\d{14}(?!\d)
.NET regex demo
(?:^|\D)(\d{14})(?:\D|$)
Here is a Live Demo

Regular expression for my password policy [duplicate]

This question already has answers here:
Regular expression to enforce complex passwords, matching 3 out of 4 rules
(4 answers)
Closed 8 years ago.
My password strength criteria is as below :
Character length: 8 -> 24
Contains least 3 of following types:
Uppercase letter [A-Z]
Lowercase letter [a-z]
Numeric [0-9]
Special character ~`!##$%^&*()-_=+[{]}\|;:'"<,>.?/
Can anyone help me to make regular expression and explain. Thanks.
It would be very difficult to do in one regex. Personally, I'd check for each case separately and count if you've got three matches. It would be easier to read and maintain.
So match [A-Z] then [a-z] then [0-9] and finally [~`!##$%^&*()-_=+[{]}\|;:'"<,>.?/]
If you end up with a match in three of the tests that's success.
Use this Regex:
(?=.{8,24})(?=.*\d)(?=.*[A-Za-z])(?=.*[~`!##\$%\^&\*()_=\+[{\]}\|;:'"<,>\.\?/]).*
Debuggex Demo

Categories