#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2006
    Posts
    124
    Rep Power
    16

    Regexp with positive look-ahead


    Hi,
    I need to evaluate a string; the string should be atleast 6 characters in length, consist of letters and numbers and at least 2 of those characters should be numbers.

    As far as I know, I need something like a 'positive look-ahead' to achive this? I googled around a bit, used some existing patterns and finally added a bit of my own basic knowledge of regular expressions and came up with this:

    Code:
    ^(?=.*[0-9])[a-z0-9]{6,}$
    Which actually works fine, but also validates strings with only one number in it.

    Can anyone tell me what I need to change to make it work like I want it to?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    Personally, I would do this in two steps. First verify the string is 6 alphanumerics, then count the instances of digits. Putting everything in one regex works for simple cases but can easily grow out of control.

    Anyway, the solution to your problem is in your look ahead that finds a digit, look for a second digit after the first with some number of intervening characters.

    Comments on this post

    • Andreas19 agrees
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2006
    Posts
    124
    Rep Power
    16
    Originally Posted by OmegaZero
    Anyway, the solution to your problem is in your look ahead that finds a digit, look for a second digit after the first with some number of intervening characters.
    Thanks for your reply, unfortunately you are overestimating my Regex capabilities. Do you know what exactly I need to change to get the desired result?
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,994
    Rep Power
    9397
    Your lookahead only finds one number. Modify it so that it finds two numbers.

    Hint: look for any amount of non-numbers, a digit, more non-numbers, and another digit.

    Comments on this post

    • Andreas19 agrees
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2006
    Posts
    124
    Rep Power
    16
    Originally Posted by OmegaZero
    Anyway, the solution to your problem is in your look ahead that finds a digit, look for a second digit after the first with some number of intervening characters.
    Originally Posted by requinix
    Hint: look for any amount of non-numbers, a digit, more non-numbers, and another digit.
    Now that I read OmegaZero's post again, and the hint from requinix, I finally "got it".
    Code:
    ^(?=.*[0-9].*[0-9])[a-z0-9]{6,}$
    I don't know if this Regex is perfect, but it does exactly what I want it to.
    -> Reputation given to both of you.
    Last edited by Andreas19; September 27th, 2008 at 11:05 PM.

IMN logo majestic logo threadwatch logo seochat tools logo