Java gets a method based on a regular expression that specifies the value of the attribute specified by the HTML tag

  • 2020-05-27 05:41:51
  • OfStack

This article demonstrates an example of how Java gets the value of a specified HTML tag's specified attribute based on a regular expression. I will share it with you for your reference as follows:

Sometimes there may be a requirement to get the specified attribute value of the specified tag from the HTML page, which can be parsed through the third party library, but this is relatively cumbersome!

If you use regular expressions, it becomes simple. The code is as follows:

package com.mmq.regex;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 * @use  Access to specify HTML The value of the specified attribute of the tag 
 * @ProjectName stuff
 * @Author mikan
 * @FullName
 * @JDK 1.6.0
 * @Version 1.0
public class MatchHtmlElementAttrValue {
   *  Access to specify HTML The value of the specified attribute of the tag 
   * @param source  The source text to match 
   * @param element  Tag name 
   * @param attr  The attribute name of the tag 
   * @return  Attribute value list 
  public static List<String> match(String source, String element, String attr) {
    List<String> result = new ArrayList<String>();
    String reg = "<" + element + "[^<>]*?\\s" + attr + "=['\"]?(.*?)['\"]?(\\s.*?)?>";
    Matcher m = Pattern.compile(reg).matcher(source);
    while (m.find()) {
      String r =;
    return result;
  public static void main(String[] args) {
    String source = "<a title= China sports daily  href=''>aaa</a><a title=' Beijing daily ' href=''>bbb</a>";
    List<String> list = match(source, "a", "title");

PS: here are two more handy regular expression tools for you to use:

JavaScript regular expression online testing tool:

Online regular expression generation tool:

I hope this article has been helpful to you in java programming.

Related articles: