Exemple d’expression régulière de nom de domaine

Exemple d'expression régulière de nom de domaine

domain name

Modèle d'expression régulière de nom de domaine

^((?!-)[A-Za-z0-9-]{1,63}(?

Le modèle ci-dessus garantit que le nom de domaine correspond aux critères suivants:

  1. Le nom de domaine doit être a-z | A-Z | 0-9 et tiret (-)

  2. Le nom de domaine doit comporter entre 1 et 63 caractères

  3. Le dernier Tld doit comporter au moins deux caractères et un maximum de 6 caractères

  4. Le nom de domaine ne doit pas commencer ou se terminer par un tiret (-) (par exemple -google.com or google-.com)

  5. Le nom de domaine peut être un sous-domaine (par exemple example.blogspot.com)

La description

^               #Start of the line
 (              #Start of group #1
    (?! -)          #Can't start with a hyphen
    [A-Za-z0-9-]{1,63}  #Domain name is [A-Za-z0-9-], between 1 and 63 long
    (?

Note
Ce modèle d'expression régulière devrait pouvoir correspondre à la plupart des noms de domaine «réels».

Liste des noms de domaine valides

  1. www.google.com

  2. google.com

  3. example123.com

  4. example-info.com

  5. sub.example.com

  6. sub.example-info.com

  7. example.com.au

  8. g.co

  9. example.t.t.co

Liste des noms de domaine invalides et pourquoi.

  1. example.t.t.c - Tld must between 2 and 6 long

  2. exemple, com - La virgule n'est pas autorisée

  3. exemple - Pas de Tld

  4. example.123 , Tld not allow digit

  5. .com - Doit commencer par [A-Za-z0-9]

  6. example.com/users - No Tld

  7. -example.com - Cannot begin with a hyphen -

  8. example-.com - Cannot end with a hyphen -

  9. sub.-example.com - Ne peut pas commencer par un trait d'union -

  10. sub.example-.com - Cannot end with a hyphen -

1. Exemple d'expression régulière Java

Un exemple Java simple pour valider un nom de domaine avec le modèle d'expressions régulières ci-dessus.

DomainUtils.java

package com.example.regex;

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

public class DomainUtils {

    private static Pattern pDomainNameOnly;
    private static final String DOMAIN_NAME_PATTERN = "^((?!-)[A-Za-z0-9-]{1,63}(?

2. Test unitaire avec jUnit

Un exemple jUnit.

DomainUtilsTestParam.java

package com.example.regex;

import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(value = Parameterized.class)
public class DomainUtilsTestParam {

    private String domain;
    private boolean expected;

    public DomainUtilsTestParam(String domain, boolean expected) {
        this.domain = domain;
        this.expected = expected;
    }

    @Parameters(name= "{index}: isValid({0})={1}")
    public static Iterable data() {
        return Arrays.asList(new Object[][] {
        { "www.google.com", true },
        { "google.com", true },
        { "example123.com", true },
        { "example-info.com", true },
        { "sub.example.com", true },
                { "sub.example-info.com", true },
                { "example.com.au", true },
                { "sub.example.com", true },
                { "sub.sub.example.com", true },
                { "g.co", true },
                { "example.t.t.co", true },
                { "example.t.t.c", false },      //Tld must at between 2 and 6 long
                { "example,com", false },    //comma not allowed
                { "example", false },        //no tld
                { "example.123", false },    //digit not allowed in tld
                { ".com", false },      //must start with [A-Za-z0-9]
                { "example.a", false },      //last tld need at least two characters
                { "example.com/users", false },  // no tld
                { "-example.com", false },   //Cannot begin with a hyphen -
                { "example-.com", false },   //Cannot end with a hyphen -
                { "sub.-example.com", false },   //Cannot begin with a hyphen -
                { "sub.example-.com", false }    //Cannot end with a hyphen -
            }
    );
     }

    @Test
    public void test_validDomains() {
    assertEquals(expected,DomainUtils.isValidDomainName(domain));
    }

}

Sortie, tout est passé.

domain-regex-junit

4. Test unitaire avec TestNG

Un exemple TestNG.

DomainUtilsTestParam.java

package com.example.regex;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DomainUtilsTestParam {

    @DataProvider
    public Object[][] ValidDomainNameProvider() {
      return new Object[][] {{
               new String[] {
        "www.google.com", "google.com",
        "example123.com", "example-info.com",
        "sub.example.com","sub.example-info.com",
        "example.com.au", "sub.example.com",
        "sub.sub.example.com", "g.co", "example.t.t.co" } } };
    }

    @DataProvider
    public Object[][] InvalidDomainNameProvider() {
      return new Object[][] { { new String[] {
        "example.t.t.c", "example,com",
        "example", "example.123",
        ".com", "example.a",
        "example.com/users", "-example.com",
        "example-.com",".com", "sub.-example.com", "sub.example-.com"} } };
    }

    @Test(dataProvider = "ValidDomainNameProvider")
    public void ValidDomainNameTest(String[] domainName) {

        for (String temp : domainName) {
            boolean valid = DomainUtils.isValidDomainName(temp);
            System.out.println("Valid domain name : " + temp);
            Assert.assertEquals(valid, true);
        }

    }

    @Test(dataProvider = "InvalidDomainNameProvider",
              dependsOnMethods = "ValidDomainNameTest")
    public void InValidDomainNameTest(String[] domainName) {

        for (String temp : domainName) {
            boolean valid = DomainUtils.isValidDomainName(temp);
            System.out.println("Invalid domain name : " + temp);
            Assert.assertEquals(valid, false);
        }
    }

}

Résultat

Valid domain name : www.google.com
Valid domain name : google.com
Valid domain name : example123.com
Valid domain name : example-info.com
Valid domain name : sub.example.com
Valid domain name : sub.example-info.com
Valid domain name : example.com.au
Valid domain name : sub.example.com
Valid domain name : sub.sub.example.com
Valid domain name : g.co
Valid domain name : example.t.t.co
Invalid domain name : example.t.t.c
Invalid domain name : example,com
Invalid domain name : example
Invalid domain name : example.123
Invalid domain name : .com
Invalid domain name : example.a
Invalid domain name : example.com/users
Invalid domain name : -example.com
Invalid domain name : example-.com
Invalid domain name : .com
Invalid domain name : sub.-example.com
Invalid domain name : sub.example-.com
PASSED: ValidDomainNameTest([Ljava.lang.String;@4661e987)
PASSED: InValidDomainNameTest([Ljava.lang.String;@117b8cf0)

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