I'm trying validate Gmail username by using their self rule on create account page.
There are the rules:
First and last character of username must be an ascii letter (a-z) or number (0-9)
Username must be between 6 and 30 characters long
Only letters (a-z), numbers (0-9), and periods (.) are allowed
Username cannot contain consecutive periods (.)
Expecting results:
.carlos.so#gmail.com - invalid
carlos.so.#gmail.com - invalid
carlos..so#gmail.com - invalid
carlos_so#gmail.com - invalid
carlos.so#gmail.com - valid
Already tried the pattern above but without success:
(?!\.)[a-zA-Z_.]{6,30}(?!.*\.)
You could use:
^[a-zA-Z0-9](?=[a-zA-Z0-9.]{5,29}#)[a-zA-Z0-9]*(?:\.[a-zA-Z0-9]+)*#gmail\.com$
^ Start of string
[a-zA-Z0-9] Match a single char a-zA-Z0-9
(?=[a-zA-Z0-9.]{5,29}#) Assert 5-29 allowed chars to the right followed by #
[a-zA-Z0-9]* Match optional chars a-zA-Z0-9
(?:\.[a-zA-Z0-9]+)* Optionally repeat matching . and 1+ allowed chars
#gmail\.com Match #gmail.com
$ End of string
See a regex101 demo.
So a partial answer (I can't leave comments on stack overflow yet), would be
^[A-Za-z0-9][A-Za-z0-9.]{5,29}[A-Za-z0-9]#gmail.com
This will handle all cases except if two or more periods occur consecutively.
Is there a reason why you need it all in a single regex, or could you use two regexes? You weren't clear, but if that is a possibility it might be a good idea.
Related
i am trying to remove all lines that its password doesnt follow my standard policy
username:password
the policy are
Passwords are after ":" And password policy is that the password must be 7 to 32 characters long and The password must contain a mix of letters, numbers, and/or special characters also passwords containing only letters or only numbers are not accepted
username:Password42
Username52#:sssdt3
user:Pass!626795
use:uss
removing all lines only leaving
username:Password42
user:Pass!626795
i tried using regex, Made this one but it doesnt work perfectly, Why so? Anyone can fix it?
^:*(?!(?=.*\d)(?=.*[a-z])(?=.*[A-Z])|(?=.*\d)(?=.*[a-z])(?=.*[!##$%^&*()_+])|(?=.*\d)(?=.*[A-Z])(?=.*[!##$%^&*()_+])|(?=.*[a-z])(?=.*[A-Z])(?=.*[!##$%^&*()_+])|(?=.*[a-z])(?=.*[A-Z])(?=.*[!##$%^&*()_+])).*$\R*
The pattern you can consider using is
^[^:\r\n]+:(?!(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{7,32}$).*\R?
See the regex demo
Details:
^ - start of a line
[^:\r\n]+ - 1 or more chars other than a colon, CR and LF
: - a colon
(?!(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{7,32}$) - negative lookahead that fails the match if there are seven to thirty-two chars other than line break chars immediately to the right of the current location, that contain both a digit, lower- or uppercase letter
.* - any 0 or more chars other than line break chars (the rest of the line)
\R? - an optional line break sequence
NOTE you may add more alternatives to the negative lookahead. E.g. replace (?=.*\d)(?=.*[a-zA-Z]) with (?:(?=.*\d)(?=.*[a-z])(?=.*[A-Z])|(?=.*\d)(?=.*[!##$%^&*()_+])) to also require digits and special chars of your choice in the pattern to negate.
I am building a secured algorithm to get rid of obfuscation attacks. The user is validated with the token which should satisfy following condition:
username in lowercase letters only and username is at least 5 digit long.
username is followed with #.
After # first two characters are important. A digit and a character always. This part contains at least a digit, a lowercase and an upperCase Letter.
In between there could be any number of digits or letters only.
In the last the digit and character should exactly match point-3's digit and character.
It should end with #.
The characters in the middle of two # should be at least 5 characters long.
The complete token consists only of two #, lowercase and uppercase letters and digits. And
I don't know about regular expression but my guide told me this task is easily achieved at validation time by regular expressions. After I looked for long on the internet and found some links which are similar and tried to combine them and got this:
^[a-z]{5,}#[a-zA-Z0-9]{2}[A-Z][0-9A-Za-z]*[a-zA-Z0-9]{2}#$
But this only matches 1 test case. I don't know how I can achieve the middle part of two hashes. I tried to explain my problem as per my english. Please help.
Below test cases should pass
userabcd#4a39A234a#
randomuser#4A39a234A#
abcduser#2Aa39232A#
abcdxyz#1q39A231q#
randzzs#1aB1a#
Below test cases should fail:
randuser#1aaa1a#
randuser#1112#
randuser#a1a1##
randuser#1aa#
u#4a39a234a#
userstre#1qqeqe123231q$
user#1239a23$a#
useabcd#4a39a234a#12
You may try:
^[a-z]{5,}#(?=[^a-z\n]*[a-z])(?=[^A-Z\n]*[A-Z])(\d[a-zA-Z])[a-zA-Z\d]*\1#$
Explanation of the above regex:
^, $ - Represents start and end of the line respectively.
[a-z]{5,} - Matches lower case user names 5 or more times.
# - Matches # literally.
(?=[^a-z]*[a-z]) - Represents a positive look-ahead asserting at least a lowercase letters.
(?=[^A-Z]*[A-Z]) - Represents a positive look-ahead asserting at least an uppercase letters.
(\d[a-zA-Z]) - Represents a capturing group matching first 2 character i.e. a digit and a letter. If you want other way then use [a-zA-Z]\d.
[a-zA-Z\d]* - Matching zero or more of the characters in mentioned character set.
\1 - Represents back-reference exactly matching the captured group.
You can find the demo of the above regex in here.
Note: If you want to match one string at a time i.e. for practical purposes; remove \n from the character sets.
You can use this regex as an alternative.
^[a-z]{5,}#(?=.*?[a-z])(?=.*?[A-Z])(\d[a-zA-Z])[a-zA-Z\d]*\1#$
Recommended reading: Principle of contrast
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
I've custom regex pattern for check correct username on url:
^[#](?:[a-z][a-z0-9_]*[a-z0-9])?$
This pattern work when I write usernames:
#username
#username_16
#username16
But not work when I write:
#u
First part of question:
How to rewrite this pattern for work in #u?
Second part of question:
How control characters limit or length after # symbol?
The [a-z] and [a-z0-9] are obligatory patterns inside the optional group, hence if there is something after #, there must be two chars at least.
Besides, your regex also matches a string that equals #.
To fix all these issues you may use
^#[a-z](?:[a-z0-9_]*[a-z0-9])?$
See the regex demo.
Now, to restrict the length of a string after # symbol, you may insert a (?=.{x,m}$) positive lookahead right after #. Say, to only match 3 or 4 chars after #, use:
^#(?=[a-z0-9_]{3,4}$)[a-z](?:[a-z0-9_]*[a-z0-9])?$
^^^^^^^^^^^^^^^^^^^
Or, since the consuming pattern will validate the rest
^#(?=.{3,4}$)[a-z](?:[a-z0-9_]*[a-z0-9])?$
^^^^^^^^^^^
See this regex demo
Details
^ - start of string
(?=.{3,4}$) - a positive lookahead that requires any 3 or 4 chars other than line break chars up to the end of the string immediately to the right of the current location (i.e. from the string start here)
# - a # char
[a-z] - a lowercase ASCII letter
(?:[a-z0-9_]*[a-z0-9])? - an optional non-capturing group matching 1 or 0 occurrences of
[a-z0-9_]* - 0+ lowercase ASCII letters, digits or _
[a-z0-9] - a lowercase ASCII letter or digits
$ - end of string.
I need regex that only allows a maximum of 2 digits (or whatever the desired limit is actually) to be entered into an input field.
The requirements for the field are as follows:
Allow a-z A-Z
Allow 0-9
Allow - and . characters
Allow spaces (\s)
Do not allow more than 2 digits
Do not allow any other special characters
I have managed to put together the following regex based on several answers on SO:
^(?:([a-zA-z\d\s\.\-])(?!([a-zA-Z]*\d.*){3}))*$
The above regex is really close. It works successfully for the following:
test 12 test
test12
test-test.12
But it allows an input of:
123 (but not 1234, so it's close).
It only needs to allow an input of 12 when only digits are entered into the field.
I would like some help in finding a more efficient and cleaner (if possible) solution than my current regex - but it must still be regex, no JS.
You could use a positive lookahead like
(?=^(?:\D*\d\D*){2}$) # only two digits
^[- .\w]+$ # allowed characters
See a demo on regex101.com.
You may use a negative lookahead anchored at the start that will make the match fail once there are 3 digits found anywhere in the string:
^(?!(?:[^0-9]*[0-9]){3})[a-zA-Z0-9\s.-]*$
^^^^^^^^^^^^^^^^^^^^^^^
See the regex demo
Details:
^ - start of string
(?!(?:[^0-9]*[0-9]){3}) - the negative lookahead failing the match if exactly 3 following sequences are found:
[^0-9]* - zero or more chars other than digits
[0-9] - a digit (thus, the digits do not have to be adjoining)
[a-zA-Z0-9\s.-]* - 0+ ASCII letters, digits, whitespace, . or - symbols
$ - end of string.