Bonus Exercise 3 completed.
This commit is contained in:
@@ -15,17 +15,7 @@ public class BonusExercises {
|
|||||||
return matcher.matches();
|
return matcher.matches();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
This method should find and return the first date in a string.
|
|
||||||
|
|
||||||
Supported formats:
|
|
||||||
- American: MM/DD/YYYY (e.g., 12/09/2023)
|
|
||||||
- British: DD/MM/YYYY (e.g., 12/09/2023 — same pattern, context matters)
|
|
||||||
- ISO: YYYY-MM-DD (e.g., 2024-07-15)
|
|
||||||
- Slash variant: YYYY/MM/DD (e.g., 2025/01/01)
|
|
||||||
|
|
||||||
If no match for a date is found in the string, return null.
|
|
||||||
*/
|
|
||||||
public String findDate(String string) {
|
public String findDate(String string) {
|
||||||
//Different types of dates
|
//Different types of dates
|
||||||
String[] datePatterns = {
|
String[] datePatterns = {
|
||||||
@@ -55,19 +45,27 @@ public class BonusExercises {
|
|||||||
return null;
|
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) {
|
public int findValidPasswords(String string) {
|
||||||
// todo
|
//This string matches the conditions for the password
|
||||||
return -1;
|
String regex = "^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[!@#$%^&*])(?!.*\\s).{8,}$";
|
||||||
|
|
||||||
|
Pattern pattern = Pattern.compile(regex);
|
||||||
|
Matcher matcher = pattern.matcher(string);
|
||||||
|
|
||||||
|
//Splitting the string by the spaces to check each one
|
||||||
|
String[] words = string.split("\\s+");
|
||||||
|
|
||||||
|
int n = 0;
|
||||||
|
|
||||||
|
//For counting the matches in the string array
|
||||||
|
for (String word : words) {
|
||||||
|
if (pattern.matcher(word).matches()) {
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user