Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

regular expression help

Options
  • 07-08-2009 2:49pm
    #1
    Registered Users Posts: 8,070 ✭✭✭


    field should be exactly eight characters long,
    start with an alphabetical character and include exactly two non-alphabetical
    characters, and no spaces.


    i have this = ?????

    var re = ^(a-z)(2=.*\d).{8}$/;


Comments

  • Registered Users Posts: 368 ✭✭backboiler


    Damn, it's ugly but will do the job, I think.
    /^[a-zA-Z][^ ]{7}$/ && /^[a-zA-Z]*[^a-zA-Z][a-zA-Z]*[^a-zA-Z][a-zA-Z]*$/

    First regexp checks the length is 8, first char is alpha and no spaces are present. Second regexp makes sure there are exactly two non-alpha. Logical-and between the two completes the mess.

    Can be simplified to
    /^[a-z][^ ]{7}$/i && /^[a-z]*[^a-z][a-z]*[^a-z][a-z]*$/i
    if the /i modifier (ignore case) is accepted by whatever language you're using.


  • Registered Users Posts: 8,070 ✭✭✭Placebo


    Thanks bb, i got this

    var i = /^([a-zA-Z])(?=(.*\d.*){2,})\w{8}$/;

    think \d should be \S

    trying to implement it in javascript but it doesnt work?
    <script type="text/javascript">
    
    var i = /^([a-zA-Z])(?=(.*\d.*){2,})\w{8}$/;
    
    var str = "aaaaaa22";
    var reg = new RegExp(i);
    
    if (!(reg.test(str))){
      alert(" Valid");}
    else{
      alert(InValid");}
    		  
    </script>
    


  • Registered Users Posts: 368 ✭✭backboiler


    Are you sure the lookahead assertion (?=) is supported in javascript? Repeat count shouldn't be {2,} anyway but {2} instead if you want exactly two digits.
    The total width required there is 9 anyway: 1 for [a-zA-Z] plus 8 for \w{8}. Change it to 7.
    There is no point in having the parentheses around the [a-zA-Z] either.
    Substituting \S for \d will prevent tabs being accepted.

    Overall it doesn't match exactly what you put in your first post since you said "no spaces" but what you have won't allow punctuation or non-printing characters either. This may be what you want but it's not what you asked for originally. :cool: Be careful when mixing character classes: \w and \S aren't the same thing so there could be holes in your assumptions.

    i don't know much about javascript really but the second alert line looks suspect too.
    Should all that fail, have you made sure you have the right syntax for the regexp. Does javascript need the leading and trailing slashes? Does it need quotes around the string? Try it with something simpler like finding a single 'A' character and make sure that works first.


  • Closed Accounts Posts: 20 boars.ie


    > field should be exactly eight characters long,
    > start with an alphabetical character and include exactly two non-alphabetical
    > characters, and no spaces.

    > var re = ^(a-z)(2=.*\d).{8}$/;

    $ perl -ne '/^(a-z)(2=.*\d).{8}$/ and print "valid\n" or print "not valid\n"' <<< 'Q22ertyu'
    not valid

    > Damn, it's ugly but will do the job, I think.
    > /^[a-zA-Z][^ ]{7}$/ && /^[a-zA-Z]*[^a-zA-Z][a-zA-Z]*[^a-zA-Z][a-zA-Z]*$/

    $ perl -ne '/^[a-zA-Z][^ ]{7}$/ && /^[a-zA-Z]*[^a-zA-Z][a-zA-Z]*[^a-zA-Z][a-zA-Z]*$/ and print "valid\n" or print "not valid\n"' <<< 'Q2ertyui'
    valid

    > Can be simplified to
    > /^[a-z][^ ]{7}$/i && /^[a-z]*[^a-z][a-z]*[^a-z][a-z]*$/i

    $ perl -ne '/^[a-z][^ ]{7}$/i && /^[a-z]*[^a-z][a-z]*[^a-z][a-z]*$/i and print "valid\n" or print "not valid\n"' <<< 'Q2ertyui'
    valid

    > Thanks bb, i got this
    > var i = /^([a-zA-Z])(?=(.*\d.*){2,})\w{8}$/;

    $ perl -ne '/^([a-zA-Z])(?=(.*\d.*){2,})\w{8}$/ and print "valid\n" or print "not valid\n"' <<< 'Q22rtyui'
    not valid


    Why not just to write a function checking the provided password,
    I guess in any decent programming language you could do with 5 lines
    of code or thereabouts?


  • Registered Users Posts: 8,070 ✭✭✭Placebo


    i ended up writing a function, it was more like 10 lines?
    thanks all for the help, il post it here tom.

    load of balls.


  • Advertisement
  • Registered Users Posts: 368 ✭✭backboiler


    boars.ie wrote: »
    >
    > Can be simplified to
    > /^[a-z][^ ]{7}$/i && /^[a-z]*[^a-z][a-z]*[^a-z][a-z]*$/i

    $ perl -ne '/^[a-z][^ ]{7}$/i && /^[a-z]*[^a-z][a-z]*[^a-z][a-z]*$/i and print "valid\n" or print "not valid\n"' <<< 'Q2ertyui'
    valid

    I think your test is a bit misleading. The "<<<" operator causes a newline to be added to your input. You shoud use "echo -n 'test_str' | perl -ne ..." or "perl -ne 'chomp; ...' <<< 'test_str'".


Advertisement