diff --git a/src/main/java/BonusExercises.java b/src/main/java/BonusExercises.java index e85ec4c..ee0b1a0 100644 --- a/src/main/java/BonusExercises.java +++ b/src/main/java/BonusExercises.java @@ -15,17 +15,7 @@ public class BonusExercises { return matcher.matches(); } - /* - This method should find and return the first date in a string. - Supported formats: - - American: MM/DD/YYYY (e.g., 12/09/2023) - - British: DD/MM/YYYY (e.g., 12/09/2023 — same pattern, context matters) - - ISO: YYYY-MM-DD (e.g., 2024-07-15) - - Slash variant: YYYY/MM/DD (e.g., 2025/01/01) - - If no match for a date is found in the string, return null. - */ public String findDate(String string) { //Different types of dates String[] datePatterns = { @@ -55,19 +45,27 @@ public class BonusExercises { return null; } - /* - given a string, implement the method to detect all valid passwords - then, it should return the count of them - a valid password has the following properties: - - at least 8 characters - - has to include at least one uppercase letter, and at least a lowercase - - at least one number and at least a special char "!@#$%^&*" - - has no white-space in it - */ public int findValidPasswords(String string) { - // todo - return -1; + //This string matches the conditions for the password + String regex = "^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[!@#$%^&*])(?!.*\\s).{8,}$"; + + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(string); + + //Splitting the string by the spaces to check each one + String[] words = string.split("\\s+"); + + int n = 0; + + //For counting the matches in the string array + for (String word : words) { + if (pattern.matcher(word).matches()) { + n++; + } + } + + return n; } /*