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

Complex Regex

Last post 08-26-2008, 9:09 PM by dipendra. 4 replies.
Sort Posts: Previous Next
  •  08-25-2008, 3:21 AM 45604

    Complex Regex

    Hi there

    I am new to Regular expression. Can i get some help with below two regular expression

    1 ) **********************************************************************  

    Maximum length of 9 characters.
    Alphanumeric characters only.
    Must have at least 4 numeric characters.
    Must have no more than 2 alphabetic characters.
    The third and fourth characters must be numeric.

    2) ***********************************************************

    Maximum length of 25 characters.
    Minimum length 2 characters (unless the family name is “O”).

     

     

    Many thanks

    Dipendra

     

  •  08-25-2008, 5:48 AM 45606 in reply to 45604

    Re: Complex Regex

    dipendra:

    Hi there

    I am new to Regular expression. Can i get some help with below two regular expression

    1 ) **********************************************************************  

    Maximum length of 9 characters.
    Alphanumeric characters only.
    Must have at least 4 numeric characters.
    Must have no more than 2 alphabetic characters.
    The third and fourth characters must be numeric.

     This could be done using a couple of successive positive look a heads:

    ^(?=[a-z0-9]{6,9}$)(?=..[0-9]{2})(?=.*?[0-9].*?[0-9].*?[0-9].*?[0-9])(?=.*?[a-z].*?[a-z]).*$

    I used [0-9] and [a-z] (no caps!) to keep the regex more or less readable. You might want to replace those with \p{Alnum} or some other meta character set available in the flavor of regex you're working with.

     

    dipendra:

    2) ***********************************************************

    Maximum length of 25 characters.
    Minimum length 2 characters (unless the family name is “O”).

    ...

     

    Not quite clear what you mean by that. 

    Something like this?

    ^(?:O|.{2,25})$

    which matches "O" or any string between 2 and 25 characters. 

    Please take the time to read:

    http://regexadvice.com/forums/thread/45439.aspx

     

    Thanks. 

  •  08-25-2008, 7:05 AM 45607 in reply to 45604

    Re: Complex Regex

    1) First regexp could be like this:

    (?xi)
    ^
              (?=.{2}\d{2})                                         (?# The third and fourth characters must be numeric.)
              (?=(?:.*\d){4})                                       (?# Must have at least 4 numeric characters.)
              (?=^(?:(?:[^a-z]*[a-z][^a-z]*){0,2}|\d+)$)    (?# Must have no more than 2 alphabetic characters.)
              [a-z\d]{4,9}                                           (?#Maximum length of 9 characters.Alphanumeric characters only.)
    $
  •  08-26-2008, 9:09 PM 45672 in reply to 45604

    Re: Complex Regex

    Thanks you for your help guys
  •  08-26-2008, 9:09 PM 45673 in reply to 45604

    Re: Complex Regex

    Thanks you for your help guys

     

    Dipendra

View as RSS news feed in XML