So i'm trying to create a Regex which does the following:
Min 12 Characters, Requires Uppercase, Requires Lowercase, Requires 2 Numeric values OR 2 Special Characters.
At the moment i have the following:
~^(?=\P{Ll}*\p{Ll})(?=\P{Lu}*\p{Lu})(?=.*[!##$%^&*()]|\D*\d).{12,}~u
Which does 1 numeric OR 1 special character, not 2. I've tried adding {2} to the OR condition, however, this requires a combination of two which is incorrect.
Any help would be appreciated.
You should replace (?=.*[!##$%^&*()]|\D*\d) lookahead with (?:(?=(?:[^!##$%^&*()]*[!##$%^&*()]){2})|(?=(?:\D*\d){2})). The regex will look like
'~^(?=\P{Ll}*\p{Ll})(?=\P{Lu}*\p{Lu})(?:(?=(?:[^!##$%^&*()]*[!##$%^&*()]){2})|(?=(?:\D*\d){2})).{12,}$~u'
See the regex demo.
The lookahead matches a location that is immediately followed with
(?:[^!##$%^&*()]*[!##$%^&*()]){2} - two repetitions of any 0+ chars other than !##$%^&*() chars followed with a char from the !##$%^&*() list
| - or
(?=(?:\D*\d){2} - two repetitions of any 0+ non-digit chars followed with a digit
Related
I have a few strings and I need some help with constructing Regex to match them.
The example strings are:
AAPL10.XX1.XX2
AAA34CL
AAXL23.XLF2
AAPL
I have tried few expressions but couldn't achieve exact results. They are of the following:
[0-9A-Z]+\.?[0-9A-Z]$
[A-Z0-9]*\.?[^.]$
Following are some of the points which should be maintained:
The pattern should only contain capital letters and digits and no small letters are allowed.
The '.' in the middle of the text is optional. And the maximum number of times it can appear is only 2.
It should not have any special characters at the end.
Please ask me for any clarification.
You can write the pattern as:
^[A-Z\d]+(?:\.[A-Z\d]+){0,2}$
The pattern matches:
^ Start of string
[A-Z\d]+ Match 1+ chars A-Z or a digit
(?:\.[A-Z\d]+){0,2} Repeat 0 - 2 times a . and 1+ chars A-Z or a digit
$ End of string
Regex demo
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 am trying to create a regex which should be able to accept the following strings
proj_asdasd_000.gz.xml
proj_asdasd.gz.xml
basically 2nd underscore is optional and if any value follows it, it should only be integer.
Following is my Regex that i am trying.
^proj([a-zA-z0-9]?)+_[a-zA-z]+(_[0-9]?)+\.[a-z]+.[a-z]
Any suggestion to make it accept the above mentioned strings?
You may use
^proj[a-zA-Z0-9]*_[a-zA-Z]+(?:_[0-9]+)?\.[a-z]+\.[a-z]+$
^proj[a-zA-Z0-9]*_[a-zA-Z]+(?:_[0-9]+)?(?:\.[a-z]+){2}$
See the regex demo
Details
^ - start of string
proj - a literal substring
[a-zA-Z0-9]* - 0 or more alphanumeric chars
_ - a _ char
[a-zA-Z]+ - 1+ ASCII letters
(?:_[0-9]+)? - an optional sequence of an underscore followed with 1+ digits
\.[a-z]+\.[a-z]+ = (?:\.[a-z]+){2} - two occurrences of . and 1+ lowercase ASCII letters
$ - end of string.
Notes:
[A-z] matches more than just ASCII letters
([a-zA-z0-9]?)+ matches an optional character 1 or more times, which makes little sense. Either match a char 1 or more times with + or 0 or more times with *, no need of parentheses
(_[0-9]?)+ matches 1 or more sequences of _ followed by a single optional digit (so, it matches _9___1_, for example). The quantifiers must be swapped to match an optional sequence of _ and 1+ digits.
How can i write a regexp, that will check if string starts and ends with digits and in between contains only digits and comas? Comas must also be separated from each other with at least one digit. For the conditions above i have following regexp: ^\d(,?\d)*$ but i have following additional condition: All comma separated integers, that are composed by sequences of digits, must be different from each other. What would be the regexp that allows only this kind of strings?
Thank you
First of all, your regex contains unquantified \d, and that matches only single digits. You need to add + after \d to match 1 or more digits.
To avoid having duplicate values, you may use
^(?!.*\b(\d+)\b.*\b\1\b)\d+(?:,\d+)*$
^^^^^^^^^^^^^^^^^^^^^^^
See the regex demo
The (?!.*\b(\d+)\b.*\b\1\b) is a negative lookahead that will fail the match if after any 0+ chars other than line break chars, there is a group of digits that appear later in the string (after another 0+ chars other than line break chars) again.
Details
^ - start of string
(?!.*\b(\d+)\b.*\b\1\b) - a negative lookahead that fails the match if identical values appear in the text
\d+ - 1+ digits
(?:,\d+)* - zero or more occurrences of
, - a comma
\d+ - 1+ digits
$ - end of string.
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.