So überprüfen Sie die E-Mail-Adresse mit regulären Ausdrücken

So überprüfen Sie die E-Mail-Adresse mit regulären Ausdrücken

image

Muster für reguläre E-Mail-Ausdrücke

^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*
      @[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$;

Beschreibung

^           #start of the line
  [_A-Za-z0-9-\\+]+ #  must start with string in the bracket [ ], must contains one or more (+)
  (         #   start of group #1
    \\.[_A-Za-z0-9-]+   #     follow by a dot "." and string in the bracket [ ], must contains one or more (+)
  )*            #   end of group #1, this group is optional (*)
    @           #     must contains a "@" symbol
     [A-Za-z0-9-]+      #       follow by string in the bracket [ ], must contains one or more (+)
      (         #         start of group #2 - first level TLD checking
       \\.[A-Za-z0-9]+  #           follow by a dot "." and string in the bracket [ ], must contains one or more (+)
      )*        #         end of group #2, this group is optional (*)
      (         #         start of group #3 - second level TLD checking
       \\.[A-Za-z]{2,}  #           follow by a dot "." and string in the bracket [ ], with minimum length of 2
      )         #         end of group #3
$           #end of the line

Die Kombination bedeutet, dass die E-Mail-Adresse mit „_A-Za-z0-9 - \ +“ beginnen muss, optional gefolgt von „. [_A-Za-z0-9-]“ und mit einem „@“ -Symbol enden muss. Der Domainname der E-Mail muss mit „A-Za-z0-9-“ beginnen, gefolgt von Tld der ersten Ebene (.com, .net) „. [A-Za-z0-9]“ und optional gefolgt von Tld der zweiten Ebene (.com.au, .com.my) "\. [A-Za-z] \ {2,}", wobei Tld der zweiten Ebene mit einem Punkt "." beginnen muss. und Länge muss mindestens 2 Zeichen betragen.

1. Beispiel für einen regulären Java-Ausdruck

Hier ist ein Java-Beispiel, das Ihnen zeigt, wie Sie mit Regex die E-Mail-Adresse überprüfen.

EmailValidator.java

package com.example.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmailValidator {

    private Pattern pattern;
    private Matcher matcher;

    private static final String EMAIL_PATTERN =
        "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
        + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

    public EmailValidator() {
        pattern = Pattern.compile(EMAIL_PATTERN);
    }

    /**
     * Validate hex with regular expression
     *
     * @param hex
     *            hex for validation
     * @return true valid hex, false invalid hex
     */
    public boolean validate(final String hex) {

        matcher = pattern.matcher(hex);
        return matcher.matches();

    }
}

3. Ungültige E-Mails

1. Beispiel - muss das Symbol "@"
2 enthalten. Beispiel @ .com.my - tld kann nicht mit dem Punkt "."
3 beginnen. [email protected] - ".a" ist kein gültiges tld, das letzte tld muss mindestens zwei Zeichen
4 enthalten. Beispiel123 @ .com - tld kann nicht mit dem Punkt "."
5 beginnen. Beispiel123 @ .com.com - tld kann nicht mit dem Punkt "."
6 beginnen. .[email protected] - Das erste Zeichen der E-Mail kann nicht mit dem Punkt "."
7 beginnen. Beispiel ()@gmail.com – email’s is only allow character, digit, underscore and dash
8. example@%
.com - E-Mail tld erlaubt nur Zeichen und Ziffern
9. [email protected] – double dots “.” are not allow
10. [email protected] – email’s last character can not end with dot “.”
11. example@[email protected] – double “@” is not allow
12. [email protected].1a -email’s tld which has two characters can not contains digit

4. Gerätetest

Hier ist ein Komponententest mittestNG.

EmailValidatorTest.java

package com.example.regex;

import org.testng.Assert;
import org.testng.annotations.*;

/**
 * Email validator Testing
 *
 * @author example
 *
 */
public class EmailValidatorTest {

    private EmailValidator emailValidator;

    @BeforeClass
    public void initData() {
        emailValidator = new EmailValidator();
    }

    @DataProvider
    public Object[][] ValidEmailProvider() {
        return new Object[][] { { new String[] { "[email protected]",
            "[email protected]", "[email protected]",
            "[email protected]", "[email protected]",
            "[email protected]", "[email protected]",
            "[email protected]", "[email protected]",
            "[email protected]" } } };
    }

    @DataProvider
    public Object[][] InvalidEmailProvider() {
        return new Object[][] { { new String[] { "example", "[email protected]",
            "[email protected]", "[email protected]", "[email protected]",
            "[email protected]", "example()*@gmail.com", "example@%*.com",
            "[email protected]", "[email protected]",
            "example@[email protected]", "[email protected]" } } };
    }

    @Test(dataProvider = "ValidEmailProvider")
    public void ValidEmailTest(String[] Email) {

        for (String temp : Email) {
            boolean valid = emailValidator.validate(temp);
            System.out.println("Email is valid : " + temp + " , " + valid);
            Assert.assertEquals(valid, true);
        }

    }

    @Test(dataProvider = "InvalidEmailProvider", dependsOnMethods = "ValidEmailTest")
    public void InValidEmailTest(String[] Email) {

        for (String temp : Email) {
            boolean valid = emailValidator.validate(temp);
            System.out.println("Email is valid : " + temp + " , " + valid);
            Assert.assertEquals(valid, false);
        }
    }
}

Hier ist das Ergebnis des Unit-Tests.

Email is valid : [email protected] , true
Email is valid : [email protected] , true
Email is valid : [email protected] , true
Email is valid : [email protected] , true
Email is valid : [email protected] , true
Email is valid : [email protected] , true
Email is valid : [email protected] , true
Email is valid : [email protected] , true
Email is valid : [email protected] , true
Email is valid : [email protected] , true
Email is valid : example , false
Email is valid : [email protected] , false
Email is valid : [email protected] , false
Email is valid : [email protected] , false
Email is valid : [email protected] , false
Email is valid : [email protected] , false
Email is valid : example()*@gmail.com , false
Email is valid : example@%*.com , false
Email is valid : [email protected] , false
Email is valid : [email protected] , false
Email is valid : example@[email protected] , false
Email is valid : [email protected] , false
PASSED: ValidEmailTest([Ljava.lang.String;@15f48262)
PASSED: InValidEmailTest([Ljava.lang.String;@789934d4)

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================