Bonus Exercise 1 completed.
This commit is contained in:
@@ -5,22 +5,10 @@ import java.util.regex.Pattern;
|
|||||||
|
|
||||||
public class BonusExercises {
|
public class BonusExercises {
|
||||||
|
|
||||||
/*
|
|
||||||
complete the method below, so it will validate an email address
|
|
||||||
1. Must have exactly one @ (no more, no less)
|
|
||||||
2. Split into local-part and domain (before @ and after @)
|
|
||||||
3. Local-part rules:
|
|
||||||
- Can't be empty
|
|
||||||
- Can't start or end with dot
|
|
||||||
- Can't have two dots in a row
|
|
||||||
4. Domain rules:
|
|
||||||
- Can't be empty
|
|
||||||
- Can't start or end with hyphen
|
|
||||||
- Can't have underscores
|
|
||||||
- Each segment (between dots) must follow same hyphen rules
|
|
||||||
*/
|
|
||||||
public boolean validateEmail(String email) {
|
public boolean validateEmail(String email) {
|
||||||
String regex = ""; // todo
|
//This string matches the conditions for the email
|
||||||
|
String regex = "^[a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+)*@[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9](\\.[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])+$";
|
||||||
|
|
||||||
Pattern pattern = Pattern.compile(regex);
|
Pattern pattern = Pattern.compile(regex);
|
||||||
Matcher matcher = pattern.matcher(email);
|
Matcher matcher = pattern.matcher(email);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user