Skip to content

Regex pull request #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion AP1403 - RegEx/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>org.project</groupId>
<artifactId>RegEx</artifactId>
<artifactId>regEx</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
Expand Down
85 changes: 45 additions & 40 deletions AP1403 - RegEx/src/main/java/Exercises.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> findPalindromes(String string) {
List<String> 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<String> findPalindromes(String string) {
List<String> list = new ArrayList<>();
// todo
return list;
return palindromes;
}

public static void main(String[] args) {
// you can test your code here
}
}
}
1 change: 0 additions & 1 deletion AP1403 - RegEx/src/test/java/TestPalindromes.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestPalindromes {
Expand Down