Results 1 to 4 of 4
  1. #1
    egrizzly is offline Member
    Join Date
    Jun 2016
    Posts
    10
    Rep Power
    0

    Default Homework: Code to Validate password

    Hello all,

    We're supposed to write a Regex (Regular Expression) code to validate a password for homework. The password should have at least:

    - one number
    - one small letter
    - one capital letter
    - and one special character (!~#$)

    Can somebody please explain this for me. My attempted code is below however it doesn't work.


    String Regex = [0-9]+[a-z]+[A-Z]+[?=\\S+$]

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Homework: Code to Validate password

    I think you're honest by indicating this is homework. If this is given by your Java instructor, (s)he should be slapped in the face. This has nothing to do with Java. Regexes can grow extremely complex. Now you have two problems... If you solved this, please post the result, and I will post mine. This will show the readers how complex it will actually get.

    Now, for your problem. Your current regex requires one or more digits, followed by one or more lowercase letter, followed by one or more uppercase letters, etc. So now your regex requires a strict order, which you don't want. You need to use lookaheads, so you can check a string before you match it. I'll give you one, and you can probably figure out the rest.

    ^(?=.*[a-z]+).*$

    Explanation:
    ^ Beginning of the line
    (?=.*[a-z]+) This is a lookahead that will match zero or more characters, followed by one or more lowercase letters.
    .* Matches the entire password.
    $ End of the line

    You can create several lookaheads and chain them. If one lookahead fails, Pattern.compile(regex).matcher(password).matches() will return false.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Homework: Code to Validate password

    Just so you know, it's very easy to try your regexes at RegExr: Learn, Build, & Test RegEx. Instant testing and good explanations. (No, not affiliated)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Homework: Code to Validate password

    Quote Originally Posted by SurfMan View Post
    If this is given by your Java instructor, (s)he should be slapped in the face.
    LoL!! Made my day!

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Help Testing My Homework Code
    By sehudson in forum New To Java
    Replies: 1
    Last Post: 03-01-2011, 07:23 AM
  2. can someone validate my code?
    By alacn in forum New To Java
    Replies: 6
    Last Post: 08-08-2010, 03:18 AM
  3. Replies: 0
    Last Post: 03-28-2010, 09:55 AM
  4. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum New To Java
    Replies: 2
    Last Post: 11-14-2008, 07:53 PM
  5. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 0
    Last Post: 11-14-2008, 01:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •