• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1338
  • Last Modified:

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.
0
pwdavies
Asked:
pwdavies
  • 3
  • 2
1 Solution
 
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
 
mikelittlewoodCommented:
if length( s) <> 8 then exit;

should be

if length( s) < 8 then exit;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
b0lsc0ttCommented:
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
 
b0lsc0ttCommented:
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now