How to validate HTML tag with regular expression in java

HTML tag Regular Expression Pattern

<("[^"]*"|'[^']*'|[^'">])*>

Description

<                #start with opening tag "<"
(                #   start of group #1
"[^"]*"  #        allow string with double quotes enclosed - "string"
|                #        ..or
'[^']*'  #        allow string with single quote enclosed - 'string'
|                #        ..or
[^'">]   #        cant contains one single quotes, double quotes and ">"
)                #   end of group #1
*                # 0 or more
>                #end with closing tag ">"

 Code:

package com.codewr.javacore.regular.expression;

 

import java.util.regex.Matcher;

import java.util.regex.Pattern;

 

/**

 *

 * @author codewr

 */

public class HTMLTagValidator {

    private static final String HTML_TAG_PATTERN  = "<(\"[^\"]*\"|'[^']*'|[^'\">])*>";

 

    public static void main(String[] args) {

         HTMLTagValidator hTMLTagValidator = new HTMLTagValidator();

        //check  html tag validate

        boolean isValid = hTMLTagValidator.validate("<b>");

        System.out.println("email valid is " + isValid);

    }

 

    /**

     * Validate html tag with regular expression

     *

     * @param tag html tag for validation

     * @return true valid html tag, false invalid html tag

     */

    public boolean validate(final String tag) {

        Pattern pattern = Pattern.compile(HTML_TAG_PATTERN );

        Matcher matcher = pattern.matcher(tag);

        return matcher.matches();

 

    }

}

Code UnitTest:

Html tag valid:

1. “<b>” , “<input value=’>’>”
2. “<input value='<‘>” , “<b/>”
3. “<a href=’http://www.google.com’>”
4. “<br>” , “<br/>”
5. “<input value=\”\” id=’test’>” , “<input value=” id=’test’>”

Html tag invalid:

1. “<input value=\” id=’test’>” – one double quotes string is not allow
2. “<input value=’ id=’test’>” – one single quotes string is not allow
3. “<input value=>>” – single opening tag > is not allow , have to enclose with single or double quotes

import com.codewr.javacore.regular.expression.HTMLTagValidator;

import org.testng.Assert;

import org.testng.annotations.*;

 

/**

 * HTMLTag validator Testing

 * @author codewr

 *

 */

public class HTMLTagValidatorTest {

 

       private HTMLTagValidator htmlTagValidator;

 

       @BeforeClass

        public void initData(){

              htmlTagValidator = new HTMLTagValidator();

        }

      

       @DataProvider

       public Object[][] ValidHTMLTagProvider() {

          return new Object[][]{

                 new Object[] {"<b>"},

                   new Object[] {"<input value='>'>"},

                 new Object[] {"<input value='<'>"},

                 new Object[] {"<b/>"},

                   new Object[] {"<a href='http://www.google.com'>"},

                 new Object[] {"<br>"},

                   new Object[] {"<br/>"},

                 new Object[] {"<input value=\"\" id='test'>"},

                   new Object[] {"<input value='' id='test'>"}

          };

       }

      

       @DataProvider

       public Object[][] InvalidHTMLTagProvider() {

           return new Object[][]{

                new Object[] {"<input value=\" id='test'>"},

                new Object[] {"<input value=' id='test'>"},

                new Object[] {"<input value=>>"}

           };

       }

      

       @Test(dataProvider = "ValidHTMLTagProvider")

       public void ValidHTMLTagTest(String tag) {

             

           boolean valid = htmlTagValidator.validate(tag);

           System.out.println("HTMLTag is valid : " + tag + " , " + valid);

           Assert.assertEquals(true, valid);

      

       }

      

       @Test(dataProvider = "InvalidHTMLTagProvider",

                 dependsOnMethods="ValidHTMLTagTest")

       public void InValidHTMLTagTest(String tag) {

             

          boolean valid = htmlTagValidator.validate(tag);

          System.out.println("HTMLTag is valid : " + tag + " , " + valid);

          Assert.assertEquals(false, valid);

      

       }

}

Output:

Running HTMLTagValidatorTest

HTMLTag is valid : <b> , true

HTMLTag is valid : <input value='>'> , true

HTMLTag is valid : <input value='<'> , true

HTMLTag is valid : <b/> , true

HTMLTag is valid : <a href='http://www.google.com'> , true

HTMLTag is valid : <br> , true

HTMLTag is valid : <br/> , true

HTMLTag is valid : <input value="" id='test'> , true

HTMLTag is valid : <input value='' id='test'> , true

HTMLTag is valid : <input value=" id='test'> , false

HTMLTag is valid : <input value=' id='test'> , false

HTMLTag is valid : <input value=>> , false

Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.527 sec

Results :

Tests run: 12, Failures: 0, Errors: 0, Skipped: 0

Note: file pom.xml add maven

<dependency>

<groupId>org.testng</groupId>

<artifactId>testng</artifactId>

<version>6.8</version>

<scope>test</scope>

</dependency>