Using ExecRegExpr

I need a valid regular expression that enforces
1.      minimum of 8 characters long.
2.      Must contain at least 1 numerical.
3.      Must contain at lower case.
4.      Must contain at upper case.

I have been given the code below but it raises an exception(and i am not sure that it is what is described).

  if ExecRegExpr('(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$', 'Monkey' ) then


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike LittlewoodEngineerCommented:
Dear god, why would anyone even use some of these functions?
You might as well re-write it in assembly they are that unreadable, especially if you then had to hand your code over to someone else.
What you are asking for would be fairly easy to code your own routine.
Mike LittlewoodEngineerCommented:
I'm guessing
3.      Must contain at lower case.
4.      Must contain at upper case.
should be
3.      Must contain at least 1 lower case.
4.      Must contain at least 1 upper case.

Below is a quick and dirty way to do what you need.
function TForm1.isValidExpression( const s: string): boolean;
   hasNumber, hasLowerCase, hasUpperCase: boolean;
   i: smallInt;
   result := false;
   if length( s) <> 8 then exit;
   hasNumber := false;
   hasLowerCase := false;
   hasUpperCase := false;
   for i := 1 to length( s) do
      if hasNumber and hasLowerCase and hasUpperCase then break;
      case s[ i] of
        '0'..'9': if not hasNumber then hasNumber := true;
        'a'..'z': if not hasLowerCase then hasLowerCase := true;
        'A'..'Z': if not hasUpperCase then hasUpperCase := true;
   result := hasNumber and hasLowerCase and hasUpperCase;

Open in new window

Mike LittlewoodEngineerCommented:
if length( s) <> 8 then exit;

should be

if length( s) < 8 then exit;
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

pwdaviesAuthor Commented:
I am aware of how to code in this.  That is not the point of the question.  I need to be able to use regular expressions as part of a password policy for muliple clients.  They decide they policy and something simular has been used for .net applictaions and a delphi equivelent is required.
b0lsc0ttIT ManagerCommented:
Delphi isn't my expertise so one of the other experts may be able to help with how to use the expression in Delphi but I can provide it.  The code below should work in Delphi (according to my utility).  The expression will make sure the string has at least 8 characters and at least one of each of the following: a number, lower case and a upper case letter.
Let me know if you have a question about it or how it works.

	Regex: TPerlRegEx;
Regex := TPerlRegEx.Create(nil);
Regex.RegEx := '\A(?=\S*?[A-Z])(?=\S*?[a-z])(?=\S*?[0-9])\S{8,}\z';
Regex.Options := [];
Regex.Subject := SubjectString;
if Regex.Match then begin
	// Successful match
else begin
	// Match attempt failed

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
b0lsc0ttIT ManagerCommented:
Please post to explain how this turned out.  Since you provided a B grade I am curious.  Did the Delphi not work?  The expression should've been fine but let me know if that wasn't the case.  Since you didn't award an A grade a post to respond and let us know why and how this turned out is hoped for (and courteous) after our work to post and respond.
Thanks for the interesting question.  I'm glad I could help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.