Regexp at least 8 symbols and only one uppercase character - regex

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]*$

Related

RegEx: How to match a whole string with fixed-length region with negative look ahead conditions that are overriden afterwards?

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 have regex but its not catch angle bracket

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.

Regex for name type

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.

Password validation using regular expressions

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.

what does this regular expression mean?

^(?!-)[a-z\d\-]{1,100}$
Here's an explanation using regex comment mode, so this expanded form can itself be used as a regex:
(?x) # flag to enable comment mode
^ # start of line/string.
(?!-) # negative lookahead for literal hyphen (-) character, so fails if the next position contains one.
[a-z\d\-] # character class matches a single alpha (a-z), digit (\d) or hyphen (\-).
{1,100} # match the above [class] upto 100 times, at least once.
$ # end of line/string.
In short, it's matching upto 100 lowercase alphanumerics or hyphen, but the first character must not be hyphen.
Could be attempting to validate a serial number, or similar, but it's too general to say for sure.
Not all regex engines support negative lookaheads. If you're trying to figure out what it is doing in order to adapt for an engine without negative lookaheads, you can use:
^[a-z\d][a-z\d-]{0,99}$
(?!-) == negative lookahead
start of line not followed by a - that contains at least 1 to 100 characters that can be a-z or 0-9 or a - followed by the end of the line, though the \d in the character class is probably wrong and should be specified by 0-9 otherwise the a-z takes care of a 'd' character, depends on the regex flavor.
A string of letters, digits and dashes. Between 1 and 100 characters. The first character is not a dash.