Solved

Regular expression with special characters

Posted on 2011-09-29
4
327 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now