From e98ac631d248daf05ac32c71080c7869b12c554a Mon Sep 17 00:00:00 2001 From: remuslum <57741494+remuslum@users.noreply.github.com> Date: Thu, 9 Nov 2023 21:21:44 +0800 Subject: [PATCH] Altered regex --- .../seedu/address/model/person/Email.java | 22 +++++++++---------- .../seedu/address/model/person/EmailTest.java | 15 ++++++------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/main/java/seedu/address/model/person/Email.java b/src/main/java/seedu/address/model/person/Email.java index af4697a5742..2b06301eeff 100644 --- a/src/main/java/seedu/address/model/person/Email.java +++ b/src/main/java/seedu/address/model/person/Email.java @@ -10,6 +10,12 @@ public class Email { private static final String SPECIAL_CHARACTERS = "+_.-"; + // alphanumeric and special characters + private static final String ALPHANUMERIC_NO_UNDERSCORE = "[^\\W_]+"; // alphanumeric characters except underscore + private static final String LOCAL_PART_REGEX = "^" + ALPHANUMERIC_NO_UNDERSCORE + "([" + SPECIAL_CHARACTERS + "]" + + ALPHANUMERIC_NO_UNDERSCORE + ")*"; + public static final String VALIDATION_REGEX = LOCAL_PART_REGEX + "@" + "([a-zA-Z]+\\.)+[a-zA-Z]{2,6}$"; + public static final String MESSAGE_CONSTRAINTS = "Emails should be of the format local-part@domain " + "and adhere to the following constraints:\n" + "1. The local-part should only contain alphanumeric characters and these special characters, excluding " @@ -18,21 +24,13 @@ public class Email { + "2. This is followed by a '@' and then a domain name. The domain name is made up of domain labels " + "separated by periods.\n" + "The domain name must:\n" - + " - end with a domain label at least 2 characters long\n" - + " - have each domain label start and end with alphanumeric characters\n" - + " - have each domain label consist of alphanumeric characters, separated only by hyphens, if any."; - // alphanumeric and special characters - private static final String ALPHANUMERIC_NO_UNDERSCORE = "[^\\W_]+"; // alphanumeric characters except underscore - private static final String LOCAL_PART_REGEX = "^" + ALPHANUMERIC_NO_UNDERSCORE + "([" + SPECIAL_CHARACTERS + "]" - + ALPHANUMERIC_NO_UNDERSCORE + ")*"; - private static final String DOMAIN_PART_REGEX = ALPHANUMERIC_NO_UNDERSCORE - + "(-" + ALPHANUMERIC_NO_UNDERSCORE + ")*"; - private static final String DOMAIN_LAST_PART_REGEX = "(" + DOMAIN_PART_REGEX + "){2,}$"; // At least two chars - private static final String DOMAIN_REGEX = "(" + DOMAIN_PART_REGEX + "\\.)*" + DOMAIN_LAST_PART_REGEX; - public static final String VALIDATION_REGEX = LOCAL_PART_REGEX + "@" + DOMAIN_REGEX; + + " - end with a domain label that is between 2 to 6 characters (e.g. .com, .edu)\n" + + " - have each domain label start and end with characters only\n"; + public final String value; + /** * Constructs an {@code Email}. * diff --git a/src/test/java/seedu/address/model/person/EmailTest.java b/src/test/java/seedu/address/model/person/EmailTest.java index 2e3628951eb..7847ebbaeb0 100644 --- a/src/test/java/seedu/address/model/person/EmailTest.java +++ b/src/test/java/seedu/address/model/person/EmailTest.java @@ -52,27 +52,26 @@ public void isValidEmail() { assertFalse(Email.isValidEmail("peterjack@-example.com")); // domain name starts with a hyphen assertFalse(Email.isValidEmail("peterjack@example.com-")); // domain name ends with a hyphen assertFalse(Email.isValidEmail("peterjack@example.c")); // top level domain has less than two chars + assertFalse(Email.isValidEmail("test@localhost")); // alphabets only + assertFalse(Email.isValidEmail("123@145")); // numeric domain name + assertFalse(Email.isValidEmail("a1+be.d@example1.com")); // mixture of alphanumeric and special characters + assertFalse(Email.isValidEmail("peter_jack@very-very-very-long-example.com")); // domain name has symbols // valid email assertTrue(Email.isValidEmail("PeterJack_1190@example.com")); // underscore in local part assertTrue(Email.isValidEmail("PeterJack.1190@example.com")); // period in local part assertTrue(Email.isValidEmail("PeterJack+1190@example.com")); // '+' symbol in local part assertTrue(Email.isValidEmail("PeterJack-1190@example.com")); // hyphen in local part - assertTrue(Email.isValidEmail("a@bc")); // minimal - assertTrue(Email.isValidEmail("test@localhost")); // alphabets only - assertTrue(Email.isValidEmail("123@145")); // numeric local part and domain name - assertTrue(Email.isValidEmail("a1+be.d@example1.com")); // mixture of alphanumeric and special characters - assertTrue(Email.isValidEmail("peter_jack@very-very-very-long-example.com")); // long domain name assertTrue(Email.isValidEmail("if.you.dream.it_you.can.do.it@example.com")); // long local part assertTrue(Email.isValidEmail("e1234567@u.nus.edu")); // more than one period in domain } @Test public void equals() { - Email email = new Email("valid@email"); + Email email = new Email("valid@email.com"); // same values -> returns true - assertTrue(email.equals(new Email("valid@email"))); + assertTrue(email.equals(new Email("valid@email.com"))); // same object -> returns true assertTrue(email.equals(email)); @@ -84,7 +83,7 @@ public void equals() { assertFalse(email.equals(5.0f)); // different values -> returns false - assertFalse(email.equals(new Email("other.valid@email"))); + assertFalse(email.equals(new Email("other.valid@email.com"))); } @Test