自定义电子邮件Regex问题

时间:2017-06-14 10:45:49

标签: java regex

我有两个正则表达式,一个是MCC,另一个是CAPI允许在MCC中的东西也应该允许有CAPI类型的要求。我做了更改,但两个测试用例都失败了。请帮我搞定

public class EmailValidationTest {
public static final String MCC_REGEX = "^(\\w+)([\\-+.\\'][\\w]+)*@(\\w[\\-\\w]*\\.){1,5}([A-Za-z]){2,6}$";

public static final String CAPI_REGEX="^(?!.*'')[a-zA-Z_]+[_\'+A-Za-z0-9-\\+]+(?<!')+(\\.[_A-Za-z0-9-\'+]+(?<!'))*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";


@Test
public void testValidEmailId() {
    String emailStr = "_tom.moddy@company.com";
    Assert.assertTrue("Invalid email-id",emailStr.matches(MCC_REGEX));
    Assert.assertTrue("Invalid email-id",emailStr.matches(CAPI_REGEX));
}

@Test
public void testValidEmailIdWithApostrophe() {
    String emailStr = "to'm.moddy@company.com";
    Assert.assertTrue("Invalid email-id",emailStr.matches(MCC_REGEX));
    Assert.assertTrue("Invalid email-id",emailStr.matches(CAPI_REGEX));
}

@Test
public void testInvalidEmailIdStartWithApostrophe() {
    String emailStr = "'tom.moddy@company.com";
    Assert.assertFalse("Invalid email-id",emailStr.matches(MCC_REGEX));
    Assert.assertFalse("Invalid email-id",emailStr.matches(CAPI_REGEX));
}

@Test
public void testInvalidEmailIdEndWithApostrophe() {
    String emailStr = "tom.moddy'@company.com";
    Assert.assertFalse("Invalid email-id",emailStr.matches(MCC_REGEX));
    Assert.assertFalse("Invalid email-id",emailStr.matches(CAPI_REGEX));
}

@Test
public void testInvalidEmailIdWithConsecutiveApostrophe() {
    String emailStr = "tom.modd''y@company.com";
    Assert.assertFalse("Invalid email-id",emailStr.matches(MCC_REGEX));
    Assert.assertFalse("Invalid email-id",emailStr.matches(CAPI_REGEX));
}

@Test
public void testInvalidEmailIdApostropheInDomain() {
    String emailStr = "tom.moddy@compan'y.com";
    Assert.assertFalse("Invalid email-id",emailStr.matches(MCC_REGEX));
    Assert.assertFalse("Invalid email-id",emailStr.matches(CAPI_REGEX));
}

@Test
public void testValidEmailIdWithSymbol1() {
    String emailStr = "tom.mod-dy@company.com";
    Assert.assertTrue("Invalid email-id",emailStr.matches(MCC_REGEX));
    Assert.assertTrue("Invalid email-id",emailStr.matches(CAPI_REGEX));
}

@Test
public void testInvalidEmailIdWithSymbol3() {
    String emailStr = "tom.mod$dy@company.com";
    Assert.assertFalse("Invalid email-id",emailStr.matches(MCC_REGEX));
    Assert.assertFalse("Invalid email-id",emailStr.matches(CAPI_REGEX));
}

@Test
public void testInvalidEmailIdWithSymbol4() {
    String emailStr = "tom.mod'.dy@company.com";
    Assert.assertFalse("Invalid email-id",emailStr.matches(MCC_REGEX));
    Assert.assertFalse("Invalid email-id",emailStr.matches(CAPI_REGEX));
}

@Test
public void testValidEmailIdWithSymbol2() {
    String emailStr = "tom.mod+dy@company.com";
    Assert.assertTrue("Invalid email-id",emailStr.matches(MCC_REGEX));
    Assert.assertTrue("Invalid email-id",emailStr.matches(CAPI_REGEX));
}

@Test
public void testInvalidEmailIdWithSymbol5() {
    String emailStr = "tom.moddy+@company.com";
    Assert.assertFalse("Invalid email-id",emailStr.matches(MCC_REGEX));
    Assert.assertFalse("Invalid email-id",emailStr.matches(CAPI_REGEX));
}

@Test
public void testInvalidEmailIdWithSymbol6() {
    String emailStr = "+tom.moddy@company.com";
    Assert.assertFalse("Invalid email-id",emailStr.matches(MCC_REGEX));
    Assert.assertFalse("Invalid email-id",emailStr.matches(CAPI_REGEX));
}

@Test
public void testValidEmailIdWithSymbol7() {
    String emailStr = "tom+moddy@company.com";
    Assert.assertTrue("Invalid email-id",emailStr.matches(MCC_REGEX));
    Assert.assertTrue("Invalid email-id",emailStr.matches(CAPI_REGEX));
}

@Test
public void testInvalidEmailIdWithSymbol8() {
    String emailStr = "tom'+moddy@company.com";
    Assert.assertFalse("Invalid email-id",emailStr.matches(MCC_REGEX));
    Assert.assertFalse("Invalid email-id",emailStr.matches(CAPI_REGEX));
}

}

testInvalidEmailIdWithSymbol5testInvalidEmailIdWithSymbol8此测试用例失败,CAPI正则表达式失败。

0 个答案:

没有答案