diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/First-Assignment-RegEx.iml b/.idea/First-Assignment-RegEx.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/First-Assignment-RegEx.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..7f4b56a --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..60c7054 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..cb61c96 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..9438c0f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ 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..6e691e4 100644 --- a/AP1403 - RegEx/src/main/java/Exercises.java +++ b/AP1403 - RegEx/src/main/java/Exercises.java @@ -5,55 +5,106 @@ 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,7}$"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.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 + String regex = "\\b(\\d{1,2}[/-]\\d{1,2}[/-]\\d{2,4}|\\d{4}[/-]\\d{1,2}[/-]\\d{1,2})\\b"; + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(string); + + while (matcher.find()) { + String date = matcher.group(); + + String[] parts; + if (date.contains("/")) { + parts = date.split("/"); + } else if (date.contains("-")) { + parts = date.split("-"); + } else { + continue; + } + + int day, month, year; + if (parts[0].length() == 4) { + year = Integer.parseInt(parts[0]); + month = Integer.parseInt(parts[1]); + day = Integer.parseInt(parts[2]); + } else { + day = Integer.parseInt(parts[0]); + month = Integer.parseInt(parts[1]); + year = Integer.parseInt(parts[2]); + } + + if (month < 1 || month > 12) { + continue; + } + if (day < 1 || day > 31) { + continue; + } + if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) { + continue; + } + if (month == 2) { + boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); + if (day > 29 || (day == 29 && !isLeapYear)) { + continue; + } + } + + return date; + } + 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; - } + String regex = "(?=\\S{8,})(?=\\S*[a-z])(?=\\S*[A-Z])(?=\\S*\\d)(?=\\S*[!@#$%^&*])\\S+"; + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(string); - /* - you should return a list of *words* which are palindromic - by word we mean at least 3 letters with no whitespace in it + int count = 0; + while (matcher.find()) { + String password = matcher.group(); + if (!password.matches(".*\\s.*")) { + count++; + } + } + return count; + } - note: your implementation should be case-insensitive, e.g. Aba -> is palindrome - */ public List findPalindromes(String string) { List list = new ArrayList<>(); - // todo + + String regex = "\\b[a-zA-Z]{3,}\\b"; + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(string); + + while (matcher.find()) { + String word = matcher.group(); + String lowerCaseWord = word.toLowerCase(); + + boolean isPalindrome = true; + int length = lowerCaseWord.length(); + for (int i = 0; i < length / 2; i++) { + if (lowerCaseWord.charAt(i) != lowerCaseWord.charAt(length - 1 - i)) { + isPalindrome = false; + break; + } + } + + if (isPalindrome) { + list.add(word); + } + } + return list; } - public static void main(String[] args) { - // you can test your code here } } diff --git a/First-Assignment-RegEx/AP1403 - RegEx/.idea/workspace.xml b/First-Assignment-RegEx/AP1403 - RegEx/.idea/workspace.xml new file mode 100644 index 0000000..66478c6 --- /dev/null +++ b/First-Assignment-RegEx/AP1403 - RegEx/.idea/workspace.xml @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + + + + 1741376726574 + + + + + + \ No newline at end of file