My regular expresiion is not working in C# - c#

I need a regex that checks if a password contains at least one Lowercase letter, at least one Upper case letter, at least two numbers and at least one of(_*&$). This is a MVC project.
This what i have
[RegularExpression(#"(?=\.\*\\d{2})(?=\./*[a-z])(?=\.\*[A-Z])(?=.*[_*&$])", ErrorMessage = "The password must contain at least 1 letter, 2 digits and a special symbol (_*&$)")]

There are a lot of issues with the current regex:
You escaped . and *, and now they denote literal . and * chars, while you wanted to use them as special regex metacharacters
To match at least two digits, you can't just use a \d{2} pattern because it does not match non-consecutive digits, you need \d.*\d or a more efficient (?:\D*\d){2}
You are only using lookaheads, non-consuming patterns, but the RegularExpressionAttribute requires a full string to match the pattern.
Thus, you need
#"^(?=(?:\D*\d){2})(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=[^_*&$]*[_*&$]).*"
Details
^ - start of string
(?=(?:\D*\d){2}) - two not necessarily consecutive digits
(?=[^a-z]*[a-z]) - at least one lowercase ASCII letter
(?=[^A-Z]*[A-Z]) - at least one uppercase ASCII letter
(?=[^_*&$]*[_*&$]) - at least one special char from the _*&$ set
.* - the whole string (with no line breaks) (it gets consumed).

Related

Regularexpression for duplicate pattern

I am trying to write a regex to handle these cases
contains only alphanumeric with minimum of 2 alpha characters(numbers are optional).
only special character allowed is hyphen.
cannot be all same letter ignoring hyphen.
cannot be all hyphens
cannot be all numeric
My regex: (?=[^A-Za-z]*[A-Za-z]){2}^[\w-]{6,40}$
Above regex works for most of the scenarios except 1) & 3).
Can anyone suggest me to fix this. I am stuck in this.
Regards,
Sajesh
Rule 1 eliminates rule 4 and 5: It can neither contain only hyphens, nor only digits.
/^(?=[a-z\d-]{6,40}$)[\d-]*([a-z]).*?(?!\1)[a-z].*$/i
(?=[a-z\d-]{6,40}$) look ahead for specified characters from 6 to 40
([a-z]).*?(?!\1)[a-z] checks for two letters and at least one different
See this demo at regex101
This pattern with i flag considers A and a as the "same" letter (caseless matching) and will require another alpbhabet. For case sensitive matching here another demo at regex101.
You can use
^(?!\d+$)(?!-+$)(?=(?:[\d-]*[A-Za-z]){2})(?![\d-]*([A-Za-z])(?:[\d-]*\1)+[\d-]*$)[A-Za-z\d-]{6,40}$
See the regex demo. If you use it in C# or PHP, consider replacing ^ with \A and $ with \z to make sure you match the entire string even in case there is a trailing newline.
Details:
^ - start of string
(?!\d+$) - fail the match if the string only consists of digits
(?!-+$) - fail the match if the string only consists of hyphens
(?=(?:[\d-]*[A-Za-z]){2}) - there must be at least two ASCII letters after any zero or more digits or hyphens
(?![\d-]*([A-Za-z])(?:[\d-]*\1)+[\d-]*$) - fail the match if the string contains two or more identical letters (the + after (?:[\d-]*\1) means there can be any one letter)
[A-Za-z\d-]{6,40} - six to forty alphanumeric or hyphen chars
$ - end of string. (\z might be preferable.)

Regular expression that Must have at least one letter

I have a case where I am using a queue of regular expressions to filter out specific items in an Observer pattern. The filter will place the values in specific controls based on their values. However 1 of the controls pattern is that it can accept ANY ASCII Character. Let me list the filters in their order with the RegEx
Column Rule Regex
Receiving 7 DIGITS #"^[1-9]([0-9]{6}$)" --->Works
Count 2 digits, no leading 0 #"^[1-9]([0-9]{0,1})$" --->Works
Producer any ASCII char. #".*" --->too broad
MUST contain a letter
Is there a regular expression that will accept any set of ASCII characters, but 1 of them MUST be a letter (upper or lower case)?
#"^(?=.*[A-Za-z])$" -->Didn't work
examples that would need to go into expression
123 red
red
123 red123
red - 123
red
If you want to match the whole rang of ASCII chars you may use
#"^(?=[^A-Za-z]*[A-Za-z])[\x00-\x7F]*$"
If only printable chars are allowed use
#"^(?=[^A-Za-z]*[A-Za-z])[ -~]*$"
Note the (?=[^A-Za-z]*[A-Za-z]) positive lookahead is located right after ^, that is, it is only triggered at the start of a string. It requires an ASCII letter after any 0 or more chars other than an ASCII letter.
Your ^(?=.*[A-Za-z])$ pattern did not work because you wanted to match an empty string (^$) that contains (?=...) at least one ASCII letter ([A-Za-z]) after any 0+ chars other than newline (.*).
You could try [A-Za-z]+.
It matches when there is at least one letter. You want something more specific?
How about
^.*[a-zA-Z]+.*$ ?
Between start and end of line, accept any number of any characters, then at least one a-z/A-Z character, then again any number of any characters.

Regex for first name

I am quite new to regex thing and need regex for first name which satisfies following conditions:
First Name must contain letters only. It may contain spaces, hyphens, or apostrophes.
It must begin with letters.
All other characters and numbers are not valid.
Special characters ‘ and – cannot be together (e.g. John’-s is not allowed)
An alphabet should be present before and after the special characters ‘ and – (e.g. John ‘s is not allowed)
Two consecutive spaces are not allowed (e.g. Annia St is not allowed)
Can anyone help? I tried this ^([a-z]+['-]?[ ]?|[a-z]+['-]?)*?[a-z]$ but it's not working as expected.
Regexes are notoriously difficult to write and maintain.
One technique that I've used over the years is to annotate my regexes by using named capture groups. It's not perfect, but can greatly help with the readability and maintainability of your regex.
Here is a regex that meets your requirements.
^(?<firstchar>(?=[A-Za-z]))((?<alphachars>[A-Za-z])|(?<specialchars>[A-Za-z]['-](?=[A-Za-z]))|(?<spaces> (?=[A-Za-z])))*$
It is split down into the following parts:
1) (?<firstchar>(?=[A-Za-z])) This ensures the first character is an alpha character, upper or lowercase.
2) (?<alphachars>[A-Za-z]) We allow more alpha chars.
3) (?<specialchars>[A-Za-z]['-](?=[A-Za-z])) We allow special characters, but only with an alpha character before and after.
4) (?<spaces> (?=[A-Za-z])) We allow spaces, but only one space, which must be followed by alpha characters.
You should use a testing tool when writing regexes, I'd recommend https://regex101.com/
You can see from the screenshot below how this regex performs.
Take the regex I've given you, run it in https://regex101.com/ with samples you'd like to match against, and tweak it to fit your requirements. Hopefully I've given you enough information to be self sufficient in customising it to your needs.
You can use this link to run the regex https://regex101.com/r/O2wFfi/1/
Edit
I've updated to address the issue in your comment, rather than just give you the code, I will explain the problem and how I fixed it.
For your example "Sam D'Joe", if we run the original regex, the following happens.
^(?<firstchar>[A-Za-z])((?<alphachars>[A-Za-z])|(?<specialchars>[A-Za-z]['-][A-Za-z])|(?<spaces> [A-Za-z]))*$
1) ^ matches the start of the string
2) (?<firstchar>[A-Za-z]) matches the first character
3) (?<alphachars>[A-Za-z]) matches every character up to the space
4) (?<spaces> [A-Za-z]) matches the space and the subsequent alpha char
Matches consume the characters that they match
This is where we run into a problem. Our "specialchars" part of the regex matches an alpha char, our special char and then another alpha char ((?<specialchars>[A-Za-z]['-](?=[A-Za-z]))).
The thing you need to know about regexes, is each time you match a character, that character is then consumed. We've already matched the alpha char before the special character, so our regex will never match.
Each step actually looks like this:
1) ^ matches the start of the string
2) (?<firstchar>[A-Za-z]) matches the first character
3) (?<alphachars>[A-Za-z]) matches every character up to the space
4) (?<spaces> [A-Za-z]) matches the space and the subsequent alpha char
and then we're left with the following
We cannot match this, because one of our rules is "An alphabet should be present before and after the special characters ‘ and –".
Lookahead
Regex has a concept called "lookahead". A lookahead allows you to match a character without consuming it!
The syntax for a lookahead is ?= followed by what you want to match. E.g. ?=[A-Z] would look ahead for a single character that is an uppercase letter.
We can fix our regex, by using lookaheads.
1) ^ matches the start of the string
2) (?<firstchar>[A-Za-z]) matches the first character
3) (?<alphachars>[A-Za-z]) matches every character up to the space
4) We now change our "spaces" regex, to lookahead to the alpha char, so we don't consume it. We change (?<spaces> [A-Za-z]) to (?<spaces> ?=[A-Za-z]). This matches the space and looks ahead to the subsequent alpha char, but doesn't consume it.
5) (?<specialchars>[A-Za-z]['-][A-Za-z]) matches the alpha char, the special char, and the subsequent alpha char.
6) We use a wildcard to repeat matching our previous 3 rules multiple times, and we match until the end of the line.
I also added lookaheads to the "firstchar", "specialchars" and "spaces" capture groups, I've bolded the changes below.
^(?<firstchar>(?=[A-Za-z]))((?<alphachars>[A-Za-z])|(?<specialchars>[A-Za-z]['-](?=[A-Za-z]))|(?<spaces> (?=[A-Za-z])))*$
This short regex should do it ^([a-zA-Z]+?)([-\s'][a-zA-Z]+)*?$ ,
([a-zA-Z]+?) - Means the String should start with alphabets.
([-\s'][a-zA-Z]+)*? - Means the string must have hyphen,space or apostrophe followed by alphabets.
^ and $ - denote start and end of string
Here's the link to regex demo.
Try this one
^[^- '](?=(?![A-Z]?[A-Z]))(?=(?![a-z]+[A-Z]))(?=(?!.*[A-Z][A-Z]))(?=(?!.*[- '][- '.]))(?=(?!.*[.][-'.]))[A-Za-z- '.]{2,}$
Demo

Regex to restrict special characters in the beginning of an email address

PFB the regex. I want to make sure that the regex should not contain any special character just after # and just before. In-between it can allow any combination.
The regex I have now:
#"^[^\W_](?:[\w.-]*[^\W_])?#(([a-zA-Z0-9]+)(\.))([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$"))"
For example, the regex should not match
abc#.sj.com
abc#-.sj-.com
SSDFF-SAF#-_.SAVAVSAV-_.IP
Since you consider _ special, I'd recommend using [^\W_] at the beginning and then rearrange the starting part a bit. To prevent a special char before a #, just make sure there is a letter or digit there. I also recommend to remove redundant capturing groups/convert them into non-capturing:
#"^[^\W_](?:[\w.-]*[^\W_])?#(?:\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.|(?:[\w-]+\.)+)(?:[a-zA-Z]{2,3}|[0-9]{1,3})\]?$"
Here is a demo of how this regex matches now.
The [^\W_](?:[\w.-]*[^\W_])? matches:
[^\W_] - a digit or a letter only
(?:[\w.-]*[^\W_])? - a 1 or 0 occurrences of:
[\w.-]* - 0+ letters, digits, _, . and -
[^\W_] - a digit or a letter only
Change the initial [\w-\.]+ for [A-Za-z0-9\-\.]+.
Note that this excludes many acceptable email addresses.
Update
As pointed out, [A-Za-z0-9] is not an exact translation of \w. However, you appear to have a specific definition as to what you consider special characters and so it is probably easier for you to define within the square brackets what you class as allowable.

Regular Expression For Alphanumeric Characters and Underscores and character Limit

I would like to have a regular expression that checks if a string contains upper and lowercase letters, numbers, and underscores as well as a character limit. These are the only Types allowed in the string.
However, the string does not have to contain all of the specified arguments.
Meaning that the string could be alphanumeric or alphanumeric with underscores or just numbers or just letters ETC.
I followed the suggestions provided here: Regular Expression for alphanumeric and underscores
and came up with the following Expression: ^([a-zA-Z0-9_]){3,15}$
So the Question is: What Is wrong with my REGEX?
Your regex - ^([a-zA-Z0-9_]){3,15}$ - matches a whole string that is 3 to 15 chars long and only consists of ASCII letters, digits or _ symbol.
It appears you want to detect a string that contains at least 3 characters from the specified ranges (letter/digit/underscore).
You may use
[a-zA-Z0-9_](?:[^a-zA-Z0-9_]*[a-zA-Z0-9_]){2}
Or a less linear:
(?:[^a-zA-Z0-9_]*[a-zA-Z0-9_]){3}
Use the patterns with Regex.IsMatch, this method allows partial matches.
Details:
[a-zA-Z0-9_] - matches a single char from the ranges specified
(?: - start of a non-capturing group matching a sequence of....
[^a-zA-Z0-9_]* - 0+ chars other than those defined in the negated character class
[a-zA-Z0-9_] - a char from the ranges specified
){2} - ....2 times exactly.

Categories

Resources