Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regular expression help

Reply
Thread Tools

Regular expression help

 
 
Jon Combe
Guest
Posts: n/a
 
      09-24-2009
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.
 
Reply With Quote
 
 
 
 
Peter Makholm
Guest
Posts: n/a
 
      09-24-2009
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
 
Reply With Quote
 
 
 
 
Jon Combe
Guest
Posts: n/a
 
      09-24-2009
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57