74 lines
2.2 KiB
Java
74 lines
2.2 KiB
Java
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
public class BonusExercises {
|
|
|
|
public boolean validateEmail(String email) {
|
|
String regex = "^[a-zA-Z0-9]+[a-zA-Z0-9._]*@[a-zA-Z0-9]+[a-zA-Z0-9-]*(\\.[a-zA-Z]+)+$";
|
|
Pattern pattern = Pattern.compile(regex);
|
|
Matcher matcher = pattern.matcher(email);
|
|
|
|
return matcher.matches();
|
|
}
|
|
|
|
public String findDate(String string) {
|
|
if (string == null) {return null;}
|
|
String regex = "\\b(\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])|\\d{4}/(0[1-9]|1[0-2])/(0[1-9]|[12][0-9]|3[01])|(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/\\d{4})\\b";
|
|
Pattern pattern = Pattern.compile(regex);
|
|
Matcher matcher = pattern.matcher(string);
|
|
if (matcher.find()) {
|
|
return matcher.group();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
|
|
public int findValidPasswords(String string) {
|
|
String regex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[^A-Za-z0-9])\\S{8,}$";
|
|
Pattern pattern = Pattern.compile(regex);
|
|
Matcher matcher = pattern.matcher(string);
|
|
String[] passwords = string.split(" ");
|
|
int count = 0;
|
|
for (String pass: passwords){
|
|
if (pattern.matcher(pass).matches()){
|
|
count++;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
public List<String> findPalindromes(String string) {
|
|
List<String> list = new ArrayList<>();
|
|
String regex = "\\b[a-zA-Z]{3,}\\b";
|
|
Pattern pattern = Pattern.compile(regex);
|
|
Matcher matcher = pattern.matcher(string);
|
|
while (matcher.find()) {
|
|
if (CheckPalindrome(matcher.group())) {
|
|
list.add(matcher.group());
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
|
|
private boolean CheckPalindrome(String str) {
|
|
str = str.toLowerCase();
|
|
int i = 0;
|
|
int j = str.length() - 1;
|
|
while (i < str.length() / 2 ) {
|
|
if (str.charAt(i) != str.charAt(j)) {
|
|
return false;
|
|
}
|
|
i++;
|
|
j--;
|
|
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
// you can test your code here
|
|
}
|
|
}
|