Skip to content
Open
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
126 changes: 115 additions & 11 deletions AP1403 - RegEx/src/main/java/Exercises.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
Expand All @@ -8,8 +11,12 @@ public class Exercises {
/*
complete the method below, so it will validate an email address
*/
public boolean validateEmail(String email) {
String regex = ""; // todo

public static boolean validateEmail(String email) {

String regex = "";
regex = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\\\.[a-zA-Z]{2,}$"; // (regex).الگو برای اعتبار سنجی یک ادرس ایمیل که بر گرفته از چت جی بی تی است

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(email);

Expand All @@ -21,11 +28,36 @@ public boolean validateEmail(String email) {
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
public static String findDate(String string) {

String americanDateFormat = "\\d{2}/\\d{2}/\\d{4}";
String britishDateFormat = "\\d{2}/\\d{2}/\\d{4}"; // regex برای تشخیص تاریخ به فرمت آمریکایی و بریتانیایی باالگوی

String combinedRegex = "(" + americanDateFormat + ")|(" + britishDateFormat + ")";
Pattern pattern = Pattern.compile(combinedRegex);
Matcher matcher = pattern.matcher(string);

if (matcher.find()) {
String dateString = matcher.group();

try {
LocalDate date = LocalDate.parse(dateString, DateTimeFormatter.ofPattern("MM/dd/yyyy"));
return date.format(DateTimeFormatter.ISO_DATE);
}
catch (DateTimeParseException e) {
try {
LocalDate date = LocalDate.parse(dateString, DateTimeFormatter.ofPattern("dd/MM/yyyy"));
return date.format(DateTimeFormatter.ISO_DATE);
}
catch (DateTimeParseException ex) {
return null;
}
}
}
else{
return null;
}
}

/*
given a string, implement the method to detect all valid passwords
then, it should return the count of them
Expand All @@ -36,9 +68,19 @@ public String findDate(String string) {
- at least one number and at least a special char "!@#$%^&*"
- has no white-space in it
*/
public int findValidPasswords(String string) {
// todo
return -1;
public static int findValidPasswords(String string) {

String passwordRegex = "(?=.*[a-z])(?=.*[A-Z])(?=.*\\\\d)(?=.*[!@#$%^&*])(?=\\\\S+$).{8,}"; // الگو یافت شده از سرچ کردن

Pattern pattern = Pattern.compile(passwordRegex);
Matcher matcher = pattern.matcher(string);

int count = 0;

while (matcher.find()) {
count++;
}
return count;
}

/*
Expand All @@ -47,13 +89,75 @@ public int findValidPasswords(String string) {

note: your implementation should be case-insensitive, e.g. Aba -> is palindrome
*/
public List<String> findPalindromes(String string) {
public static List<String> findPalindromes(String string) {
List<String> list = new ArrayList<>();
// todo

String[] words = string.toLowerCase().split("\\s+");

for (String word : words) {
if (word.length() >= 3 && isPalindrome(word)) {
list.add(word);
}
}

return list;
}

//زدن یک متد اضافی برای چک کردن پالیندروم بودن یا نبودن یک حرف

private static boolean isPalindrome(String word) {
int left = 0;
int right = word.length() - 1;

while (left < right) {
if (word.charAt(left) != word.charAt(right)) {
return false;
}
left++;
right--;
}

return true;
}



public static void main(String[] args) {
// you can test your code here

Exercises validator = new Exercises();

String email1 = "test@example.com";
String email2 = "invalid-email";
String email3 = "another.test@sub.example.co.uk";

System.out.println(email1 + " is valid: " + validator.validateEmail(email1));
System.out.println(email2 + " is valid: " + validator.validateEmail(email2));
System.out.println(email3 + " is valid: " + validator.validateEmail(email3));

//چک کردن کد دوم

String text1 = "امروز تاریخ 10/25/2024 است.";
String text2 = "تاریخ جلسه 25/10/2024 است.";
String text3 = "هیچ تاریخی در این متن وجود ندارد.";

System.out.println("تاریخ در متن 1: " + findDate(text1));
System.out.println("تاریخ در متن 2: " + findDate(text2));
System.out.println("تاریخ در متن 3: " + findDate(text3));

//چک کردن کد سوم

String input = "ValidPassword123! and another Valid@1234 and one more validPass#2023 and InvalidPassword but validPass@1!";
int validPasswordCount = findValidPasswords(input);
System.out.println("Number of valid passwords: " + validPasswordCount);

// چک کردن اخرین کد

String Input = "Aba racecar level kayak notapalindrome";
List<String> palindromicWords = findPalindromes(Input);
System.out.println("Palindromic words: " + palindromicWords);



}

}