Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

REGex for 2 letters and 2 anumbers

Last post 07-05-2009, 2:10 PM by waelsch. 8 replies.
Sort Posts: Previous Next
  •  07-04-2009, 1:05 PM 54586

    REGex for 2 letters and 2 anumbers

    Dear Experts,

    My company now apply new custom password policy depends on Regular Expression and DLL Files

    DLL file isdone and I create a simple regular expression to test and it is working great.

    kindly provide me with regular expression that meets these conditions:

    1- the password should contain (at least) 2 Numbers & 2 letters of the English Alphabet, (the two letters are upper ,the two letters are  lower case . or one upper and on lower)  ,and  in any position,

    2- The length of the password is not limitied

    3- password can be started with any characters and ending with any chracters .

     to be more clear , these passwords are acceptable (P1%$0r  ,  %asw03 , 4wael5 )

     

    I appreciate your efforts 

    Best Regards, 

    Filed under: ,
  •  07-04-2009, 3:22 PM 54587 in reply to 54586

    Re: REGex for 2 letters and 2 anumbers

    You didn't mention which language you are using so this may or may not work

    Raw Match Pattern:
    ^(?=(?:.*?[a-z]){2})(?=(?:.*?\d){2}).+

    $matches Array:
    (
    [0] => Array
    (
    [0] => P1%$0r
    [1] => %asw03
    [2] => 4wael5
    )
     )
     
    NOTE: This pattern would allow whitespaces except newlines but you did say any character. 

     


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  07-04-2009, 3:53 PM 54588 in reply to 54587

    Re: REGex for 2 letters and 2 anumbers

    Thanks So much for replying

    actually the DLL file has written by visual studio , and it will compare the value that the user will write with a speceific  Regular Expression (which is sotred in Registery)

    I will check the first one , coz my value in Registry is one line only 

    sorry I was no nothing about regular expression but i read a little about it but i was going to write about 30 condition (with OR between them) but i thought that there will be an easier one , so I am asking here,

    thanks again i will check and reply.

    I know you maybe don't have time , but can you make for me a little explaination about this Regular Expression, and is in this expression the alphabatic should be before the digits, or it will match all over the world even if it is mixed like this (w1e2$%)

    Best Regards ,

    Wael 

  •  07-04-2009, 3:55 PM 54589 in reply to 54588

    Re: REGex for 2 letters and 2 anumbers

    and when I apply it , i will put a-zA-z , right coz lower or upper is acceptable
  •  07-04-2009, 4:07 PM 54590 in reply to 54589

    Re: REGex for 2 letters and 2 anumbers

    ok as I read in this forum now

    ^ start of the string

    (?=(?:.*?[a-zA-Z]){2}) when looking ahead there should be 2 or lower or upper alpha

    (?=(?:.*?\d){2}) when looking ahead there should be 2 or more Numbers

    .+             zero or more characters

    $         the end of the string

     

    now the only problem is this part  ?=(?:.*? could you please help me with understanding it correctly

     and in your answer you didn't put $ , but i think i should put it right ?? 

    Best Regards, 

  •  07-04-2009, 4:34 PM 54591 in reply to 54590

    Re: REGex for 2 letters and 2 anumbers

    Sorry I forgot to mention that the Multiline and ignorecase options should be turned on.

    Raw Match Pattern:
    ^(?=(?:.*?[a-z]){2})(?=(?:.*?\d){2}).+

    Match Pattern Explanation:
    The regular expression:

    (?im-sx:^(?=(?:.*?[a-z]){2})(?=(?:.*?\d){2}).+)

    matches as follows:

    NODE EXPLANATION
    ----------------------------------------------------------------------
    (?im-sx: group, but do not capture (case-insensitive)
    (with ^ and $ matching start and end of
    line) (with . not matching \n) (matching
    whitespace and # normally):
    ----------------------------------------------------------------------
    ^ the beginning of a "line"
    ----------------------------------------------------------------------
    (?= look ahead to see if there is:
    ----------------------------------------------------------------------
    (?: group, but do not capture (2 times):
    ----------------------------------------------------------------------
    .*? any character except \n (0 or more
    times (matching the least amount
    possible))
    ----------------------------------------------------------------------
    [a-z] any character of: 'a' to 'z'
    ----------------------------------------------------------------------
    ){2} end of grouping
    ----------------------------------------------------------------------
    ) end of look-ahead
    ----------------------------------------------------------------------
    (?= look ahead to see if there is:
    ----------------------------------------------------------------------
    (?: group, but do not capture (2 times):
    ----------------------------------------------------------------------
    .*? any character except \n (0 or more
    times (matching the least amount
    possible))
    ----------------------------------------------------------------------
    \d digits (0-9)
    ----------------------------------------------------------------------
    ){2} end of grouping
    ----------------------------------------------------------------------
    ) end of look-ahead
    ----------------------------------------------------------------------
    .+ any character except \n (1 or more times
    (matching the most amount possible))
    ----------------------------------------------------------------------
    ) end of grouping
    ----------------------------------------------------------------------

    $matches Array:
    (
    [0] => Array
    (
    [0] => P1%$0r
    [1] => %asw03
    [2] => 4wael5
    )

    )

     


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  07-04-2009, 5:00 PM 54595 in reply to 54591

    Re: REGex for 2 letters and 2 anumbers

    ok thanks very much now i understand the lines , but the new expression (?im-sx: , what this meaning sorry i didn't get it totally and should i use it ??

     sorry for two many question but i am new in Regular expression.

    Regards,

     

  •  07-04-2009, 6:37 PM 54608 in reply to 54595

    Re: REGex for 2 letters and 2 anumbers

    waelsch:

    ok thanks very much now i understand the lines , but the new expression (?im-sx: , what this meaning sorry i didn't get it totally and should i use it ??

     sorry for two many question but i am new in Regular expression.

    Regards,

     

    those turn off and on different regex options in this case multiline on, ignorecase on, singleline off, ignore pattern whitespace off.

    This syntax is specific to certain languages, which is one of many reason we ask for the language you are using with the regex.  You can use that syntax with the .Net regex engine if you wish. Those languages also allow you to set those options in the constructor for the Regex object.  Since I still don't know which particular language you are using I'll leave it to you to decide on how to set the options.


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  07-05-2009, 2:10 PM 54682 in reply to 54608

    Re: REGex for 2 letters and 2 anumbers

    ok thanks very much

    it works great

    Regards, 

View as RSS news feed in XML