forked from Advanced-Programming-1403/First-Assignment-RegEx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercises.java
More file actions
92 lines (79 loc) · 3.15 KB
/
Exercises.java
File metadata and controls
92 lines (79 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
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 ="^[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) {
String regex = "(0[1-9]|[12][0-9]|3[01])[-/](0[1-9]|1[0-2])[-/](\\d{4})|\\d{4}[-/](0[1-9]|1[0-2])[-/](0[1-9]|[12][0-9]|3[01])";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);
if (matcher.find()) {
return matcher.group();
}
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) {
String regex = "(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[!@#$%^&*])[^\\s]{8,}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);
int v = 0;
String[] words = string.split(" ");
for (String word : words) {
if (matcher.reset(word).find()) {
v++;
}
}
return v;
}
/*
you should return a list of *words* which are palindromic
by word we mean at least 3 letters with no whitespace in it
note: your implementation should be case-insensitive, e.g. Aba -> is palindrome
*/
public List<String> findPalindromes(String string) {
List<String> list = new ArrayList<>();
string = string.replaceAll("[^a-zA-Z ]", "");
String[] words = string.split("\\s+");
for (String word : words) {
if (word.length() >= 3) {
String lowerWord = word.toLowerCase();
String reversed = new StringBuilder(lowerWord).reverse().toString();
if (lowerWord.equals(reversed)) {
list.add(word);
}
}
}
return list;
}
public static void main(String[] args) {
Exercises ex = new Exercises();
System.out.println(ex.validateEmail("example@example.com"));
System.out.println(ex.findDate("12/09/2023"));
System.out.println(ex.findValidPasswords("P@ssw0rd test Invalid Pass1"));
List<String> palindromes = ex.findPalindromes("Madam, did you see Bob running? Kayak radar civic level");
System.out.println(palindromes);
}
}