Solved

Regular expression with special characters

Posted on 2011-09-29
4
329 Views
Last Modified: 2012-05-12
Hello,

I want to use a regular expression to validate a password. The password must meet the next requirements:
1. at least 8 characters
2. at least one digit
3. at least one character of the set  !@#$%&+{}[]:;<>?/\

What is the regular expression for these requirements?

Regards Arne
0
Comment
Question by:adiemeer
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 36813332
It may be simpler to use 3 separate regular expressions for each of the requirements, but if your regular expression notation supports lookahead, you might test all 3 with
(?=.{8})(?=.*\d).*[!@#$%&+{}\[\]:;<>?/\\]
0
 
LVL 4

Expert Comment

by:rbride
ID: 36813340
I'd use three separate expressions, to put it into 1 is quite tricky and not worth the effort because it is hard to maintain. Here I have used perl style.

# Match at least 8 non-space characters. After the comma you can also put in the maximum length allowed, e.g. {8,20}
($pw =~ /\S{8,}/)

# At least one digit
($pw =~ /\d+/)

# and match at least one of the special characters. Note you will have to escape certain characters (done here with backslash).
($pw =~ /[!@#\$%&+{}\[\]:;<>?\\\/]+/)
0
 
LVL 10

Expert Comment

by:ienaxxx
ID: 36813443
^.*(?=.{8,})(?=.*\d)(?=.*[a-zA-Z])(?=.*[@#$%^&+=]).*$


look for which chars needs to be escaped in your language.

HTH. Bye!
0
 

Author Closing Comment

by:adiemeer
ID: 36813511
Thanks, this works fine!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question