Regular expression help

Discussion in 'Perl Misc' started by Jon Combe, Sep 24, 2009.

  1. Jon Combe

    Jon Combe Guest

    I realise this is not the ideal group, because there doesn't seem to
    be a regular expressions group but since most Perl developers use
    regular expressions I'm hoping someone will know! I use regular
    expressions a lot but I have come accross a request that has me
    stumped. The following needs to be done in a single regular
    expression:-

    Validate length to a minimum and maximum number of characters
    Must contain at least one vowel
    Must not contain any numbers
    Must contain no more than 2 adjacent repeated characters (so aa is OK,
    but aaa is not)

    I can write a single regular expression to do 2 of those but I cannot
    come up with a single regular expression to do all 4. Is it actually
    possible? I realise it could easily be done with several expressions
    and an "and" statement (and probably faster too), but this needs to be
    plugged in to an application that accepts only a single regular
    expression.

    Thanks.
    Jon.
     
    Jon Combe, Sep 24, 2009
    #1
    1. Advertising

  2. Jon Combe <> writes:

    > Validate length to a minimum and maximum number of characters
    > Must contain at least one vowel
    > Must not contain any numbers
    > Must contain no more than 2 adjacent repeated characters (so aa is OK,
    > but aaa is not)


    You can do it with a lot of look-ahead assertions:

    /
    ^
    (?= .* [aeiouy] ) # At least one vowel
    (?! .* [0-9] ) # Does not contain number
    (?! .* (\w)\1\1 ) # no more than 2 adjacent repeated characters
    .{5,8} # Between 5 and 8 chars
    $
    /x

    But the variant of regular expressions your application is using must
    have both positive and negative lookaheads for this to work.

    //Makholm
     
    Peter Makholm, Sep 24, 2009
    #2
    1. Advertising

  3. Jon Combe

    Jon Combe Guest

    > You can do it with a lot of look-ahead assertions:
    >
    > /
    >   ^
    >     (?= .* [aeiouy] )  # At least one vowel
    >     (?! .* [0-9] )     # Does not contain number
    >     (?! .* (\w)\1\1 )  # no more than 2 adjacent repeated characters
    >     .{5,8}             # Between 5 and 8 chars
    >   $
    > /x
    >
    > But the variant of regular expressions your application is using must
    > have both positive and negative lookaheads for this to work.
    >
    > //Makholm


    Thank you so much Makholm that does exactly what I want. I was already
    starting to use look ahead but for the no more than 2 adjacent
    repeated characters I was getting in a mess trying to use look behind
    assertions which obviously aren't needed.

    Jon.
     
    Jon Combe, Sep 24, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anand

    Regular Expression help...

    Anand, Jul 9, 2003, in forum: Perl
    Replies:
    1
    Views:
    1,247
    Eric J. Roode
    Jul 9, 2003
  2. Eric B.
    Replies:
    1
    Views:
    442
    Jim Gibson
    Dec 17, 2004
  3. VSK
    Replies:
    2
    Views:
    2,399
  4. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    886
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    12,053
    vforvikash
    Dec 29, 2008
Loading...

Share This Page