Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AP1403 - RegEx/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 40 additions & 6 deletions AP1403 - RegEx/src/main/java/Exercises.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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;
}

Expand All @@ -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;
}

/*
Expand All @@ -49,11 +61,33 @@ public int findValidPasswords(String string) {
*/
public List<String> findPalindromes(String string) {
List<String> 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.
"""));
}

}