0

recently they sent me an exercise to check a password with regular expressions, which would have to have a capital letter, lower case letters, and two numbers, since I have no experience with regex I created a class to experiment with, what happened was that I found the solution, but for separate expressions and coincidentally joining them in a single expression returns false. Could someone please help me with this? `

public class main {

    public static void main(String[] args) {

 

        String cadena="12Aaniel";

        //return true 8 characters minimum

        System.out.println("ejemplo10: "+cadena.matches(".{8,}"));

        //return true has at least one capital letter

        System.out.println("ejemplo10: "+cadena.matches("(.*[A-Z]+.*)"));

        //return true has at least 2 numbers

        System.out.println("ejemplo10: "+cadena.matches("(.*[0-9]{2,}.*)"));

        //return false all together

        System.out.println("ejemplo10: "+cadena.matches(".{8,}(.*[A-Z]+.*)(.*[0-9]{2,}.*)"));

 

    }

}

`

I need all in one expresion

  • 2
    I don't recommend that. The code is much easier to maintain and extend if the expressions are *separate* – g00se Jan 03 '23 at 17:46
  • "return true has at least 2 numbers" your expression here checks for 2 _consecutive_ numbers, that's not quite the same thing. – Andy Turner Jan 03 '23 at 17:49

0 Answers0