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
    ShowMessage('Yes')
  else
    ShowMessage('No')

Thanks,

Chris.
pwdaviesAsked:
Who is Participating?
 
b0lsc0ttConnect With a Mentor IT 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.
bol

var
	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
end
else begin
	// Match attempt failed
end;

Open in new window

0
 
mikelittlewoodCommented:
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.
0
 
mikelittlewoodCommented:
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;
var
   hasNumber, hasLowerCase, hasUpperCase: boolean;
   i: smallInt;
begin
   result := false;
 
   if length( s) <> 8 then exit;
 
   hasNumber := false;
   hasLowerCase := false;
   hasUpperCase := false;
 
   for i := 1 to length( s) do
   begin
      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;
      end;
   end;
 
   result := hasNumber and hasLowerCase and hasUpperCase;
end;

Open in new window

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

should be

if length( s) < 8 then exit;
0
 
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.
0
 
b0lsc0ttIT ManagerCommented:
pwdavies,
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.
bol
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.