Short/simplify regex with or condtion - regex

I have written the following regEx to check for the below conditions
Combination of at-least one lowerCase a-z with Uppercase A-Z
Combination of at-least one lowerCase a-z with a digit 0-9
Combination of at-least one UpperCase A-Z with a digit 0-9
((?=.*[a-z])(?=.*[A-Z]))|((?=.*[a-z])(?=.*[0-9]))|((?=.*[A-Z])(?=.*[0-9]))
valid inputs are:-
aA
a1
A1
Is there any way to short/simplify this regex?

Would ([a-z]+[A-Z])|(([a-z]+|[A-Z]+)[0-9]) be what you're looking for?

If I understand correctly, the rules could be rephrased like:
Input must consist of only alphanumerical characters
Reject any input that only consists of digits
Reject any input that only consists of lowercase letters
Reject any input that only consists of uppercase letters
Your regular expression does not check for the start/end of the input, nor does it verify that all characters are alphanumerical (.* is too broad), so it would accept inputs that are invalid.
You could do it like this:
^(?!\d*$|[A-Z]*$|[a-z]*$)[a-zA-Z\d]*$

Related

Regex To Validate A String, But The String Can't Contain n Number Of A Specific Character

Recently I ran into a validation situation I've been trying to solve with regex. The rules are as such:
Must start with a capital letter
Center of the string may be of any length
Center of the string may have any combination of upper and lower case letters and numbers
Center of the string may have up to one underscore
Must end with a number
I have attempted to match this string with the following regex:
^(?!_{2,})([A-Z][a-zA-Z0-9_]*[0-9])$
and
^(?<=_{0,1})([A-Z][a-zA-Z0-9_]*[0-9])$
Both of these attempts still match cases where there is more than one underscore present. I.E. App_l_e9 or App__le9.
How can you check to see if your regex match, I.E. the ([A-Z][a-zA-Z0-9_]*[0-9]) part contains zero or one underscore in any place within the middle of the string?
The simplest approach would probably be this
^[A-Z][a-zA-Z0-9]*_?[a-zA-Z0-9]*[0-9]$
Explanation:
^[A-Z] Must start with an uppercase letter
[a-zA-Z0-9]* A combination of uppercase and lowercase letters and numbers of any length (also 0-length)
_? Either zero or one underscore character
[a-zA-Z0-9]* Again A combination of uppercase and lowercase letters and numbers of any length (also 0-length)
[0-9]$ Must end with a number
This will accept A_9 or AA0_xY8 but for instance not aXY_34 or Aasf1__asdf5
If the underscore in the middle part must not be the first or last character of this middlepart, you can replace the * with a + like this.
^[A-Z][a-zA-Z0-9]+_?[a-zA-Z0-9]+[0-9]$
So this, won't accecept for instance A_9 anymore, but the word must at least be Ax_d9
You might also start the match with an uppercase A-Z and immediately check that the string ends with a number 0-9 using a positive lookahead to prevent catastrophic backtracking.
^[A-Z](?=.*[0-9]$)[a-zA-Z0-9]*_?[a-zA-Z0-9]*$
^ Start of string
[A-Z] Match an uppercase char A-Z
(?=.*[0-9]$) Positive lookahead to assert a digit 0-9 at the end of the string
[a-zA-Z0-9]* Optionally match any of the listed
_? Match an optional _
[a-zA-Z0-9]* Optionally match any of the listed
$ End of string
Regex demo
Or with an optional group
^[A-Z](?=.*[0-9]$)[a-zA-Z0-9]*(?:_[a-zA-Z0-9]*)?$
Regex demo

Regex: Validate if a string is [a-zA-Z0-9] only, 8+ chars, has at least one of lowercase, uppercase, digits

The question is pretty much in the title. I need to check if a string is alphanumerical only - no special characters, and that is contains at least one lowercase letter, at least one uppercase letter, at least one number.
passWORD1 validates, password2, PASSWORD3, passWORD, passWORD5*, psWD6 would not.
It is similar to Regex to check if a string contains at least A-Za-z0-9 but not an &, but does not meet all the criteria. I also could go with iterating through the criteria, but I really need a regex to feed it to validate.js module (so JS/Node), which will only throw one a single error stating all the password criteria at once)
You should try this:
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$"
^ Start of the string
(?=.*[a-z]) lowercase validation
(?=.*[A-Z]) uppercase validation
(?=.*\d) numbers validation
[a-zA-Z\d] characters allowed
{8,} minimum size (you can put the maximum after the comma)
$ End of the String
The regex you need fulfilling all your requirement is this,
^(?=[A-Z0-9]*[a-z])(?=[a-zA-Z]*[0-9])(?=[a-z0-9]*[A-Z])[a-zA-Z0-9]{8,}$
You basically need three positive look aheads to ensure meeting your three conditions of minimum presence of three kind of characters and finally consume the alphanumeric characters using a character set followed by quantifier as mentioned in the regex.
Explanation:
^ - Start of string
(?=[A-Z0-9]*[a-z]) - Look ahead to ensure at least one lower case alphabet
(?=[a-zA-Z]*[0-9]) - Look ahead to ensure at least one digit
(?=[a-z0-9]*[A-Z]) - Look ahead to ensure at least one upper case alphabet
[a-zA-Z0-9]{8,} - Captures eight or more alphanumeric characters
$ - Matches end of string
Regex Demo

Regex for pattern with hyphens

Is there a regex in Java that allows alphanumeric characters (both upper and lower case), has to start with a letter, could end with a letter or a digit and also contain hyphens in the middle?
I have ^[a-zA-Z][A-Za-z0-9-]$ but not sure if it could work for all cases.
^[A-Za-z]([A-Za-z0-9-]*[A-Za-z0-9])?$
^[A-Za-z]: starting with a letter
(...)?$: optionally followed by this group, and end in it
[A-Za-z0-9-]*: any number of letters, digits and hyphens
[A-Za-z0-9]: one letter or digit
You need point 2 or you'll miss single-letter sequences, which are also valid accorrdding to your description
With Python, I do this:
(?i)^[a-z]([a-z\d-][a-z\d])?$

Powershell - Regular expression for restrict uppercase and first lowercase followed by anything from lowercase and digit only

I am accepting a parameter to PSH file and validating using ValidatePattern.
Regular expression should restrict uppercase and first lowercase followed by anything from lowercase and digit only
Trying this regex: [ValidatePattern("**[^a-z][a-z0-9]**")].
But this does not match my requirements. I am elaborating below.
The first letter should lower case alphabet.
Restrict Uppercase.
Allow only lowercase letter and digit.
Single lowercase letter input also valid
Not sure about PowerShell, but valid regex for this is:
^[a-z][a-z0-9]*$
So please try:
[ValidatePattern("^[a-z][a-z0-9]*$")].
Let's analyze the requirements:
The first letter should lower case alphabet - [a-z] / \p{Ll}
Restrict Uppercase - Just do not use them in the pattern
Allow only lowercase letter and digit - [a-z0-9]
Single lowercase letter input also valid - add * after the [a-z0-9] pattern to match 0 or more occurrences of it
Use
^[a-z][a-z0-9]*$
See the regex demo.

RegEx - Password Strength

I'm trying to make a regex for allowing only strong passwords, strong in this case being defined as:
Must start with a letter (either uppercase or lowercase)
Must have at least 8 and up to 12 characters
Must have at least one uppercase letter
Must have at least three lowercase letters
Must have at least two numbers
Must have at least two special characters
Maximum number of identical consecutive characters is three
Now, last one is giving me trouble. How do I count consecutive characters?
For example, FOOfoo!?123 should work, but FOOOfoo!?12 should not (because or three esses).
What I've got so far:
^[A-Za-z]{1}(?=.*[A-Z]{1,})(?=.*[a-z]{3,})(?=.*[0-9]{2,})(?=.*[!?#*#&$]{2,}).{8,12}$
One more thing: something is amiss, because my regex above claims strings like FooFoo!?123 are invalid. I think it's because it only checks for one or more uppercase letters or three or more lowercase letters or numbers or specials, but I don't want that, I want that is the password contains three lowercase letters in total, it should be valid. How do I do that?
When you have so many conditions, it might be a good idea - provided your environment allows that - to split the regex and check each condition separately.
If you cannot do that, here is a free-spacing version of the fixed regex:
^ # start of string
(?=[^A-Z]*[A-Z]) # At least 1 uppercase ASCII letter
(?=(?:[^a-z]*[a-z]){3}) # at least 3 lowercase ASCII letters
(?=(?:[^0-9]*[0-9]){‌​2}) # at least 2 ASCII digits
(?=(?:[^!?#*#&$]*‌​[!?#*#&$]){2}) # at least 2 special symbols
(?!.*(‌​.)\1{2}) # No 3 consecutive characters
[A-Za-z] # An ASCII letter
.{7,‌​11} # 7 to 11 any characters but newline
$ # end of string
As a one-liner:
^(?=[^A-Z]*[A-Z])(?=(?:[^a-z]*[a-z]){3})(?=(?:[^0-9]*[0-9]){2})(?=(?:[^!?#*#&$]*[!?#*#&$]){2})(?!.*(.)\1{2})[A-Za-z].{7,11}$
See the regex demo
Notes:
Must have at least three lowercase letters and similar conditions are implemented using the principle of contrast, i.e. before [a-z], we may have 0+ opposite chars matched with [^a-z].
To match the 3 letters globally, not consecutively, we need to use a limiting quantifier on the grouping, not on the character class, thus, [a-z]{3,} (=consecutive 3 or more lowercase letters) is turned into (?:[^a-z]*[a-z]){3} (=3 sequences of non-lowercase letters followed with 1 lowercase letter).
The condition you needed is (?!.*(‌​.)\1{2}) - a negative lookahead ((?!...)) that checks for the presence of any character captured with (.) that is repeated twice after it with the \1 backreference and {2} limiting quantifier set on the backreference. And .* means that the repeated characters may appear anywhere in the string.