Skip to content

develop branch #5

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 1 commit 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
8 changes: 8 additions & 0 deletions .idea/.gitignore

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

9 changes: 9 additions & 0 deletions .idea/First-Assignment-RegEx.iml

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

13 changes: 13 additions & 0 deletions .idea/compiler.xml

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

7 changes: 7 additions & 0 deletions .idea/encodings.xml

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

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

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

14 changes: 14 additions & 0 deletions .idea/misc.xml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

113 changes: 82 additions & 31 deletions AP1403 - RegEx/src/main/java/Exercises.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> findPalindromes(String string) {
List<String> 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
}
}
65 changes: 65 additions & 0 deletions First-Assignment-RegEx/AP1403 - RegEx/.idea/workspace.xml

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