Advertisement
Ligh7_of_H3av3n

06. Validate Password

Mar 15th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.23 KB | None | 0 0
  1. package Uprajneniq;
  2.  
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class ValidatePassword {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         int n = Integer.parseInt(scanner.nextLine());
  12.  
  13.         Pattern pattern = Pattern.compile("_\\.+(?<passwordText>[A-Z][A-Za-z0-9]{4,}[A-Z])_\\.+");
  14.  
  15.         for (int i = 0; i < n; i++) {
  16.             String password = scanner.nextLine();
  17.             Matcher matcher = pattern.matcher(password);
  18.  
  19.             if (matcher.matches()) {
  20.                 String passwordContent = matcher.group("passwordText");
  21.                 String group = extractGroup(passwordContent);
  22.                 System.out.println("Group: " + group);
  23.             } else {
  24.                 System.out.println("Invalid pass!");
  25.             }
  26.         }
  27.     }
  28.  
  29.     private static String extractGroup(String password) {
  30.         StringBuilder groupBuilder = new StringBuilder();
  31.         for (char c : password.toCharArray()) {
  32.             if (Character.isDigit(c)) {
  33.                 groupBuilder.append(c);
  34.             }
  35.         }
  36.         return groupBuilder.length() > 0 ? groupBuilder.toString() : "default";
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement