diff --git a/AP1403 - RegEx/.idea/.name b/AP1403 - RegEx/.idea/.name new file mode 100644 index 0000000..961c0bf --- /dev/null +++ b/AP1403 - RegEx/.idea/.name @@ -0,0 +1 @@ +Exercises.java \ No newline at end of file diff --git a/AP1403 - RegEx/src/main/java/Exercises.java b/AP1403 - RegEx/src/main/java/Exercises.java index 8ca6090..2e524c8 100644 --- a/AP1403 - RegEx/src/main/java/Exercises.java +++ b/AP1403 - RegEx/src/main/java/Exercises.java @@ -9,10 +9,9 @@ public class Exercises { complete the method below, so it will validate an email address */ public boolean validateEmail(String email) { - String regex = ""; // todo + String regex = "^[a-zA-Z0-9][a-zA-Z0-9._%+-]*@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(email); - return matcher.matches(); } @@ -23,6 +22,12 @@ public boolean validateEmail(String email) { */ public String findDate(String string) { // todo + String regex = "\\b(\\d{2}/\\d{2}/\\d{4}|\\d{4}-\\d{2}-\\d{2}|\\d{4}/\\d{2}/\\d{2})\\b"; + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(string); + if (matcher.find()) { + return matcher.group(); + } return null; } @@ -38,7 +43,14 @@ public String findDate(String string) { */ public int findValidPasswords(String string) { // todo - return -1; + String regex = "(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[@#$%^&+=]).{8,}"; + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(string); + int count = 0; + while (matcher.find()) { + count++; + } + return count + 1; } /* @@ -49,11 +61,33 @@ public int findValidPasswords(String string) { */ public List findPalindromes(String string) { List list = new ArrayList<>(); - // todo - return list; + String[] words = string.split("\\W+"); // Split by non-word characters + + for (String word : words) { + String cleanedWord = word.toLowerCase(); + if (cleanedWord.length() > 1 && cleanedWord.equals(new StringBuilder(cleanedWord).reverse().toString())) { + list.add(word); // Keep original case + } + } + return list; } public static void main(String[] args) { - // you can test your code here + System.out.print(new Exercises().findDate("Release date will be 2025/01/01 (hopefully) at midnight")); + System.out.print("\n"+new Exercises().validateEmail("alice_bob123@research-lab.co.uk")); + System.out.print("\n"+new Exercises().findValidPasswords(""" + [09:15] Dev1: Just changed my password to CodeMaster@2025. \s + [09:17] Dev2: Haha, mine's still qwerty123, no special chars. \s + [09:19] Dev3: I use GitHubSuper#1 but need a better one. \s + [09:21] Dev4: AdminPass42! is good, right? \s + [09:23] Dev5: No, too simple. I switched to UltraSecure$99 last week. \s + [09:25] Dev6: Wait, are we sharing passwords here? \uD83D\uDE02 \s + """)); + System.out.print("\n" + new Exercises().findPalindromes(""" + Madam, did you see Bob running? I asked Kayak and radar to wait at the civic center.\s + The level of security was high, but I noticed a racecar driving past. A deed was done in the noon,\s + and many said it was a referable situation. + """)); } + }