Regex needed for password - regex

I require a regex for password field
I tried before posting but couldn't get through.
which validates the field for:
at least one special char
at least one alphabetic character
at least one numeric char?

Here is an article on how to write regex password validation strings:
http://nilangshah.wordpress.com/2007/06/26/password-validation-via-regular-expression/
In your case, you would look for something like this:
^.*(?=.{8,})(?=.*\d)(?=.*[A-Za-z])(?=.*[.,!-##$%^&+=_]).*$
This would require your password to be at least eight characters, contain a letter, a number, and a special character (one of these: .,!-##$%^&+=_)
If you are really struggling with regex, I would suggest you try something like this (free) tool for helping you build regex expressions:
http://www.radsoftware.com.au/regexdesigner/

Here is a regex that will require at least one alpha, one numeric, and one special character.
^.*(?=.*[a-z])(?=.*[A-Z])(?=.*[\W])(?=.*[\d]).*$
More info here.

Related

Custom email validation regex pattern not working properly

So I've got /.+[^\x20-\x2A\x2C\x2F\x3A-\x40\x5B-\x5E\x60\x7B-\xFF]\#[\w+-?]+(.{1})\w{2,}/ pattern I want to use for email validation on client-side, which doesn't work as expected.
I know that my pattern is simple and doesn't cover every standard possibility, but it's part of my regex training.
Local part of address should be valid only when it has at least one digit [0-9] or letter [a-zA-Z] and can be mixed with comma or plus sign or underscore (or all at once) and then # sign, then domain part, but no IP address literals, only domain names with at least one letter or digit, followed by one dot and at least two letters or two digits.
In test string form it doesn't validate a#b.com and does validate baz_bar.test+private#e-mail-testing-service..com, which is wrong - it should be vice versa - validate a#b.com and not validate baz_bar.test+private#e-mail-testing-service..com
What specific error I've got there and where?
I can't locate this, sorry..
You need to change your regex
From: .+[^\x20-\x2A\x2C\x2F\x3A-\x40\x5B-\x5E\x60\x7B-\xFF]\#[\w+-?]+(\.{1})\w{2,}
To: .+[^\x20-\x2A\x2C\x2F\x3A-\x40\x5B-\x5E\x60\x7B-\xFF]?\#[\w+-]+(\.{1})\w{2,}
Notice that I added a ? before the # sign and removed the ? from the first "group" after the # sign. Adding that ? will make your regex to know that hole "group" is not mandatory.
See it working here: https://regex101.com/r/iX5zB5/2
You're requiring the local part (before #) to be at least two characters with the .+ followed by the character class [^...]. It's looking for any character followed by another character not in the list of exclusions you specify. That explains why "a#b.com" doesn't match.
The second problem is partly caused by the character class range +-? which includes the . character. I think you wanted [-\w+?]+. (Do you really want question marks?) And then later I think you wanted to look for a literal . character but it really ends up matching the first character that didn't match the previous block.
Between the regex provided and the explanatory text I'm not sure what rules you intend to implement though. And since this is an exercise it's probably better to just give hints anyway.
You will also want to use the ^ and $ anchors to makes sure the entire string matches.

Correct regex for a password-type field

Working on password-field.
The regex expression for a field container letters (lowercase, uppercase), digits and some special characters will look like this:
^([a-z,A-Z,0-9,#,$,%,&,_,]{8,20})*$
Tell me please, how this should be modified if I want every pass phrase to have at least one lowercase, one uppercase and one digit?
For example, for 3-characters long pass it is:
'aB3' - pass
'ab3' - fail
You need to use lookaheads and also you need to remove all the commas present inside the character class.
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?\d)[a-zA-Z0-9#$%&_]{8,20}$
Note that the password must be atleast 8 and atmost 20 chars long.
DEMO
THIS ANSWER HAS BEEN ARCHIVED BECAUSE IT SUCKS.
Use the + token, which tells the engine to attempt to find one or more of the preceding token. e.g:
^(?=[a-z]+)(?=[A-Z])+(?=[0-9])+(?=[#$%&_\,\.]*)$
Then, use a length check elsewhere in the code to verify length.

Regular Expression for Password strength with one special characters except Underscore

I have the following regular expression:
^.*(?=^.{8,}$)(?=.*\d)(?=.*[!##$%^&*-])(?=.*[A-Z])(?=.*[a-z]).*$
I am using it to validate for
At least one letter
least one capital letter
least one number
least one special characters
least 8 characters
But along with this I need to restrict the underscore (_).
If I enter password Pa$sw0rd, this is validating correctly, which is true.
If I enter Pa$_sw0rd this is also validating correctly, which is wrong.
The thing is the regex is passing when all the rules are satisfied. I want a rule to restrict underscore along with above.
Any help will be very appreciable.
I think you can use a negated character class [^_]* to add this restriction (also, remove the initial .*, it is redundant, and the first look-ahead is already at the beginning of the pattern, no need to duplicate ^, and it is totally redundant since the total length limit can be checked at the end):
^(?=.*\d)(?=.*[!##$%^&*-])(?=.*[A-Z])(?=.*[a-z])[^_]{8,}$
See demo
^(?=.*?\d)(?=.*?[!##$%^&*-])(?=.*?[A-Z])(?=.*?[a-z])(?!.*_).{8,}$
You can try this..* at start is of no use.See demo.
https://regex101.com/r/pG1kU1/34

How to accept a dot within email address with Regex?

I have this regex [a-z0-9]*#metu\.edu which checks user input userside, using HTML5. I want to accept a dot (.) within the username, only a single dot. Such as: herp.derp#metu.edu
use below
^[a-z0-9]+[.]?[a-z0-9]+#metu\.edu$
DEMO
[a-z0-9][a-z0-9.]*#metu\.edu\.tr
This will require at least one (lower case character or number) and then (lower case character or number or dot), so email addresses cannot begin with or only consist of a single dot. However you should probably reconsider using this regex for email validations, as the acceptable email addresses contain a lot more cases (-, +, _ etc.) (and don't forget size limitations as well)
In general, e-mail addresses shouldn't be matched with regexes. However, in your specific case, it seems that you have a distinct pattern that you want to match against.
[a-z0-9]+(\.[a-z0-9]+)?#metu\.edu
Assuming that the single dot is optional, if it's mandatory, use this:
[a-z0-9]+\.[a-z0-9]+#metu\.edu
Add a dot to the character range: [a-z0-9.]*
To exclude dots at the beginning or end of the name, and only allow a single dot use multiple character classes:
[a-z0-9]+\.?[a-z0-9]+#metu\.edu
You should not be trying to parse e-mail adresses yourself, using regex, as you WILL fail. Please consider this: http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html
What I usually do is accept any string as e-mail. If it's just for user registration or whatever, there is really no need to validate it. It will fail when you send out an email, then you know it's wrong.

Regex match, quite simple:

I'm looking to match Twitter syntax with a regex.
How can I match anything that is "#______" that is, begins with an # symbol, and is followed by no spaces, just letters and numbers until the end of the word? (To tweeters, I want to match someone's name in a reply)
Go for
/#(\w+)/
to get the matching name extracted as well.
#\w+
That simple?
It should be noted that Twitter no longer allows usernames longer than 15 characters, so you can also match with:
#\w{1,15}
There are still apparently a few people with usernames longer than 15 characters, but testing on 15 would be better if you want to exclude likely false positives.
There are apparently no rules regarding whether underscores can be used the the beginning or end of usernames, multiple underscores, etc., and there are accounts with single-letter names, as well as someone with the username "_".
#[\d\w]+
\d for a digit character
\w for a word character
[] to denote a character class
+ to represent more than one instances of the character class
Note that these specifiers for word and digit characters are language dependent. Check the language specification to be sure.
There is a very extensive API for how to get valid twitter names, mentions, etc. The Java version of the API provided by Twitter can be found on github twitter-text-java. You may want to take a look at it to see if this is something you can use.
I have used it to validate Twitter names and it works very well.