Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 1st, 2006, 01:52 PM
Registered User
 
Join Date: Mar 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Regualr Expression Password Matching

Is there a way in one single Reg Expression to validate a password string that satisfies,

1. Must have at least one upper case letter [A-Z]
2. Must have at least one lower case letter [a-z]
3. Must have at least one number [0-9]
4. Must have at least one special character from [! $ % ^ & * ( ) _ + = < > ? / \ | ] [ { }]
5. The string length should be between 6 and 25.

Note: The above said steps 1-4 characters can appear in any random order.

I have a solution, but the problem with this one is, I have to check for each named group,
"^((?<lower>[a-z])|(?<upper>[A-Z])|(?<number>[0-9])|(?<symbol>[\\$\\!\\%\\^\\#]))+$"

In C# code I would check as,

if (aMatch.Success && aMatch.Groups["lower"].Success
&& aMatch.Groups["upper"].Success && aMatch.Groups["number"].Success
&& aMatch.Groups["symbol"].Success)
{
Valid Password;
}
else
Not valid;

Is there a better solution, such as writing the whole thing in one Reg Expression?

Thanks,
Chandra

 
Old March 2nd, 2006, 04:47 PM
Authorized User
 
Join Date: Mar 2006
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Chandra,

The question you have raised was an excellent one. I don't have great experiences with writing regexs, but in the light of the theoretical knowledge on the subject, following is my comment on it:

As I see it, it is possible to write a better regular expression. However, I feel it will be too complicated and probably an overkill for the problem at hand.
Since there is no specific order with which the lower-case/upper-case/numbers/sp-chars may appear, there will be 24 different terms that would have to be OR-ed.

Lemme know if you find a better way to solve this problem.


Sreeram





Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expression matching sub string mayasht Pro Java 3 August 8th, 2007 05:32 AM
Regular expression to select non-matching pattern Neuron BOOK: Beginning Regular Expressions 1 June 12th, 2006 10:06 AM
Regualr Expression Password Matching Chandra Paladugu VB.NET 1 March 24th, 2006 08:32 PM
Regular Expression for password kitkits ASP.NET 1.0 and 1.1 Basics 2 April 28th, 2005 11:44 PM
Regular Expression for password Baby_programmer ASP.NET 1.0 and 1.1 Basics 1 December 28th, 2004 01:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.