Using ExecRegExpr

Posted on 2008-11-13
Last Modified: 2012-05-05
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


Question by:pwdavies
    LVL 15

    Expert Comment

    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.
    LVL 15

    Expert Comment

    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

    LVL 15

    Expert Comment

    if length( s) <> 8 then exit;

    should be

    if length( s) < 8 then exit;

    Author Comment

    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.
    LVL 54

    Accepted Solution

    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

    LVL 54

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
    Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
    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…

    759 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

    11 Experts available now in Live!

    Get 1:1 Help Now