diff --git a/AP1403 - RegEx/pom.xml b/AP1403 - RegEx/pom.xml
index 33c537b..1233f86 100644
--- a/AP1403 - RegEx/pom.xml
+++ b/AP1403 - RegEx/pom.xml
@@ -5,7 +5,7 @@
4.0.0
org.project
- RegEx
+ regEx
1.0-SNAPSHOT
diff --git a/AP1403 - RegEx/src/main/java/Exercises.java b/AP1403 - RegEx/src/main/java/Exercises.java
index 8ca6090..c8e8731 100644
--- a/AP1403 - RegEx/src/main/java/Exercises.java
+++ b/AP1403 - RegEx/src/main/java/Exercises.java
@@ -4,56 +4,61 @@
import java.util.regex.Pattern;
public class Exercises {
-
- /*
- complete the method below, so it will validate an email address
- */
public boolean validateEmail(String email) {
- String regex = ""; // todo
- Pattern pattern = Pattern.compile(regex);
- Matcher matcher = pattern.matcher(email);
-
+ Pattern ptrn = Pattern.compile("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9]+([.-][a-zA-Z0-9]+)*\\.[a-zA-Z]{2,}$");
+ Matcher matcher = ptrn.matcher(email);
return matcher.matches();
}
-
- /*
- this method should find a date in string
- note that it should be in british or american format
- if there's no match for a date, return null
- */
+
public String findDate(String string) {
- // todo
- return null;
- }
+ String regex = "\\b(0?[1-9]|[12]\\d|3[01])[/-](0?[1-9]|1[0-2])[/-](\\d{4})\\b" // (DD/MM/YYYY)
+ + "|\\b(0?[1-9]|1[0-2])[/-](0?[1-9]|[12]\\d|3[01])[/-](\\d{4})\\b" // (MM/DD/YYYY)
+ + "|\\b(\\d{4})[/-](0[1-9]|1[0-2])[/-](0[1-9]|[12]\\d|3[01])\\b";
- /*
- given a string, implement the method to detect all valid passwords
- then, it should return the count of them
+ Pattern ptrn = Pattern.compile(regex);
+ Matcher matcher = ptrn.matcher(string);
- 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;
+ if (matcher.find()) {
+ return matcher.group();
+ }
+ return null;
+ }
+
+ public int findValidPasswords(String input) {
+ if (input == null || input.trim().isEmpty()) return 0;
+ int validCount = 0;
+ String[] passwords = input.split("\\s+");
+ String regex = "(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[!@#$%^&*])[^\\s]{8,}";
+ Pattern compiledPattern = Pattern.compile(regex);
+ for (String p : passwords) {
+ if (compiledPattern.matcher(p).matches()) {
+ validCount++;
+ }
+ }
+ return validCount;
}
+
+ public List findPalindromes(String string) {
+ List palindromes = new ArrayList<>();
+ String[] words = string.split("[^a-zA-Z0-9]+");
- /*
- you should return a list of *words* which are palindromic
- by word we mean at least 3 letters with no whitespace in it
+ for (String word : words) {
+ if (word.length() >= 3) {
+ String lowerWord = word.toLowerCase();
+ String reversed = "";
+ for (int i = lowerWord.length() - 1; i >= 0; i--) {
+ reversed += lowerWord.charAt(i);
+ }
+
+ if (lowerWord.equals(reversed)) {
+ palindromes.add(word);
+ }
+ }
+ }
- note: your implementation should be case-insensitive, e.g. Aba -> is palindrome
- */
- public List findPalindromes(String string) {
- List list = new ArrayList<>();
- // todo
- return list;
+ return palindromes;
}
-
public static void main(String[] args) {
// you can test your code here
}
-}
+}
\ No newline at end of file
diff --git a/AP1403 - RegEx/src/test/java/TestPalindromes.java b/AP1403 - RegEx/src/test/java/TestPalindromes.java
index f33b7c0..25e69d7 100644
--- a/AP1403 - RegEx/src/test/java/TestPalindromes.java
+++ b/AP1403 - RegEx/src/test/java/TestPalindromes.java
@@ -3,7 +3,6 @@
import java.util.ArrayList;
import java.util.List;
-
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestPalindromes {