I have regex :
[RegularExpression(#"^(?=.*?[A-Z])(?=(.*[a-z]){1,})(?=(.*[\d]){1,})(?=(.*[\W]){1,})(?!.*\s).{8,40}$",
ErrorMessage = "Minimum 8, max 40 characters atleast 1 Uppercase, 1 Lowercase, 1 Number and 1 Special Character")]
I want no special characters like <, >(angle brackets). So can anyone help me?
You may use the following pattern:
^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*\W)[^\s<>]{8,40}$
Demo.
Note that your original pattern had some redundant parts. For example, the capturing groups, some of the character classes, and the {1,} quantifiers; so, they were removed.
Breakdown of the pattern:
^ - Beginning of the string.
(?=.*[A-Z]) - Must contain at least one uppercase letter.
(?=.*[a-z]) - Must contain at least one lowercase letter.
(?=.*\d) - Must contain at least one digit.
(?=.*\W) - Must contain at least one non-word character.
[^\s<>]{8,40} Match between 8 and 40 characters excluding whitespace characters and angle brackets.
$ - End of the string.
Related
The strings I parse with a regular expression contain a region of fixed length N where there can either be numbers or dashes. However, if a dash occurs, only dashes are allowed to follow for the rest of the region. After this region, numbers, dashes, and letters are allowed to occur.
Examples (N=5, starting at the beginning):
12345ABC
12345123
1234-1
1234--1
1----1AB
How can I correctly match this? I currently am stuck at something like (?:\d|-(?!\d)){5}[A-Z0-9\-]+ (for N=5), but I cannot make numbers work directly following my region if a dash is present, as the negative look ahead blocks the match.
Update
Strings that should not be matched (N=5)
1-2-3-A
----1AB
--1--1A
You could assert that the first 5 characters are either digits or - and make sure that there is no - before a digit in the first 5 chars.
^(?![\d-]{0,3}-\d)(?=[\d-]{5})[A-Z\d-]+$
^ Start of string
(?![\d-]{0,3}-\d) Make sure that in the first 5 chars there is no - before a digit
(?=[\d-]{5}) Assert at least 5 digits or -
[A-Z\d-]+ Match 1+ times any of the listed characters
$ End of string
Regex demo
If atomic groups are available:
^(?=[\d-]{5})(?>\d+-*|-{5})[A-Z\d_]*$
^ Start of string
(?=[\d-]{5}) Assert at least 5 chars - or digit
(?> Atomic group
\d+-* Match 1+ digits and optional -
| or
-{5} match 5 times -
) Close atomic group
[A-Z\d_]* Match optional chars A-Z digit or _
$ End of string
Regex demo
Use a non-word-boundary assertion \B:
^[-\d](?:-|\B\d){4}[A-Z\d-]*$
A non word-boundary succeeds at a position between two word characters (from \w ie [A-Za-z0-9_]) or two non-word characters (from \W ie [^A-Za-z0-9_]). (and also between a non-word character and the limit of the string)
With it, each \B\d always follows a digit. (and can't follow a dash)
demo
Other way (if lookbehinds are allowed):
^\d*-*(?<=^.{5})[A-Z\d-]*$
demo
I need a regular expression that checks if passwords:
are at least 8 characters
contain at least 1 uppercase letter
contain at least 1 lowercase letter
contain at least 1 number or at least 1 special character
do not repeat a character more than 2 times in row
Examples that should match:
Test1234!
Te123stE
Examples that should not match:
Teeest123!
!TESTT1234
This is what I tried so far:
^(?!.*pass|.*Pass|.*qwer|.*Qwer)(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9#?!#$%^&*-]).{8,}$
I don't know how to match the repeating characters. Any ideas?
If you want to fail the match if there are 3 or more consecutive identical chars, use (?!.*(.)\1{2}) lookahead:
^(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])(?=[^0-9#?!#$%^&*-]*[0-9#?!#$%^&*-])(?!.*[pP]ass|.*[qQ]wer)(?!.*(.)\1{2}).{8,}$
See the regex demo.
If you want to fail the match if there are 3 or more not necessarily consecutive identical chars, use (?!.*(.).*\1.*\1) lookahead:
^(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])(?=[^0-9#?!#$%^&*-]*[0-9#?!#$%^&*-])(?!.*[pP]ass|.*[qQ]wer)(?!.*(.).*\1.*\1).{8,}$
See this regex demo
NOTE the changes: the most efficient lookaheads are moved to the beginning (right after ^) and the least efficient are moved to the right.
The most common lookaheads that check lower, upper case letters and special/digit chars are enhanced according to the principle of contrast.
Pattern details
^ - start of string
(?=[^A-Z]*[A-Z]) - there must be at least one uppercase ASCII letter
(?=[^a-z]*[a-z]) - there must be at least one lowercase ASCII letter
(?=[^0-9#?!#$%^&*-]*[0-9#?!#$%^&*-]) - there must be at least one special char from the 0-9#?!#$%^&*- set (digit or #, ?, !, #, $, %, ^, &, *, -)
(?!.*[pP]ass|.*[qQ]wer) - no pass/Pass and qwer/Qwer in the string allowed
(?!.*(.).*\1.*\1) - no three not necessarily consecutive repeating chars allowed
.{8,} - eight or more chars other than line break chars
$ - end of string.
You can use the following regular expression:
/
(?=.*[0-9#?!#$%^&*-]) # 1+ digit or special char
(?=.*[A-Z]) # 1+ uc letters
(?=.*[a-z]) # 1+ lc letters
(?=[a-zA-Z0-9#?!#$%^&*-]*$) # all chars valid
(?!.*(.)\1{2}) # no 3+ repeats
.{8,} # match string if length >= 8
/x # free-spacing mode
Demo
I am working on regex with the following conditions:
Must contain from 1 to 63 alphanumeric characters or hyphens.
First character must be a letter.
Cannot end with a hyphen or contain two consecutive hyphens.
I am able to get the regex like:
^[a-zA-Z0-9](?!.*--)[a-zA-Z0-9-]{0,61}[A-Za-z0-9]$
But it fails on the length constraint as well as allows patterns like "a-". How can I meet the conditions?
I would phrase your requirements as:
^(?=.{1,63}$)(?!.*--)[a-zA-Z]([a-zA-Z0-9\-]*[a-zA-Z0-9])?$
Demo
Here is a brief explanation of what each part of the above regex does:
^ from the start of the match
(?=.{1,63}$) assert that the string is between 1 63 characters
(?!.*--) assert that two hyphens do not appear together anywhere
[a-zA-Z] first character is a letter (mandatory in all matches)
([a-zA-Z0-9\-]*[a-zA-Z0-9])?
The final portion says to match a final character which is alphanumeric, but not dash, possibly preceded by alphanumeric characters or dash.
My take on this would be:
^[A-Za-z](?!.*?--)[A-Za-z0-9\-]{0,62}(?<!-)$
Try it out here
Explanation:
^ - Matches the start of the string.
[A-Za-z] - Matches the first letter.
(?!.*?--) - Ensures that there are no two consecutive hyphens in the rest of the string.
[A-Za-z0-9\-]{0,62} - Matches the remaining alphanumeric and hyphen characters.
(?<!-) - Ensures that the string doesn't end with a hyphen.
$ - Matches the end of the string.
I need a regular expression for a string with has at least 8 symbols and only one uppercase character. Java
For example, it should match:
Asddffgf
asdAsadasd
asdasdaA
But not:
adadAasdasAsad
AsdaAadssadad
asdasdAsadasdA
I tried this: ^[a-z]*[A-Z][a-z]*$ This works good, but I need at least 8 symbols.
Then I tried this: (^[a-z]*[A-Z][a-z]*$){8,} But it doesn't work
^(?=[^A-Z]*[A-Z][^A-Z]*$).{8,}$
https://regex101.com/r/zTrbyX/6
Explanation:
^ - Anchor to the beginning of the string, so that the following lookahead restriction doesn't skip anything.
(?= ) - Positive lookahead; assert that the beginning of the string is followed by the contained pattern.
[^A-Z]*[A-Z][^A-Z]*$ - A sequence of any number of characters that are not capital letters, then a single capital letter, then more non capital letters until the end of the string. This insures that there will be one and only one capital letter throughout the string.
.{8,} - Any non-newline character eight or more times.
$ - Anchor at the end of the string (possibly unnecessary depending on your requirements).
In your first regex ^[a-z]*[A-Z][a-z]*$ you could append a positive lookahead (?=[a-zA-Z]{8,}) right after the ^.
That will assert that what follows matches at least 8 times a lower or uppercase character.
^(?=[a-zA-Z]{8,})[a-z]*[A-Z][a-z]*$
I am trying to validate a password with the following rules:
Must have at least eight characters.
Must contain ONLY letters and digits.
Must contain at least two digits.
So far I wrote this code:
[0-9a-zA-Z] (?=(.*\d){2}) {8,}
Im not sure why the passwords I enter returns invalid although it follows the rules.
Remember that spaces are meaningful in a regex pattern, so you require at least 8 spaces at the end. There are no anchors in the regex, so the length limitation might not work even if you write a correct pattern. So far, this will match an alphanumeric, a space that is followed with 2 occurrences of any 0+ chars followed with a digit, but since there is space with {8,} quantifier, this pattern will never match anything.
You need
^(?=.{8})[a-zA-Z]*(?:\d[a-zA-Z]*){2}[a-zA-Z0-9]*$
See the regex demo
^ - start of string
(?=.{8}) - at least 8 chars
[a-zA-Z]* - zero or more letters
(?:\d[a-zA-Z]*){2} - 2 sequences of:
\d - a digit (may be replaced with [0-9])
[a-zA-Z]* - zero or more letters
[a-zA-Z0-9]* - 0+ alphanumeric chars
$ - end of string.
Alternatively, you may use
^(?=(?:[a-zA-Z]*\d){2})[a-zA-Z0-9]{8,}$
See another regex demo
Here, (?=(?:[a-zA-Z]*\d){2}) will require at least 2 occurrences of 0+ letters followed with a digit in the string and [a-zA-Z0-9]{8,} will match 8 or more alphanumeric chars.