正则表达式 – 匹配XML标签

开发中经常用到html标签匹配, 这里做个整理:

1.匹配标签 <[^>]+>

private static final String REGEX = "<[^>]+>";
static Pattern pattern = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE);
 Matcher matcher = pattern.matcher(content);
    while (matcher.find()) {
      MatchResult result = matcher.toMatchResult();
      System.out.println(result.group(0));
    }

2. 获取标签里面的某个属性值 attr=\”([^\”]*)\”

private static final String REGEX = "name=\"([^\"]*)\""; //取name属性
static Pattern pattern = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE);
 Matcher matcher = pattern.matcher(tag);
    if(matcher.find()) {
      MatchResult result = matcher.toMatchResult();
      System.out.println(result.group(1));
    }

3. 结合标签, 直接获取所有input标签里面的name属性: <TAG[^>]*ATTR=\”([^\”]*)\”[^>]+>

private static final String REGEX = "<input[^>]*name=\"([^\"]*)\"[^>]+>";
static Pattern pattern = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE);
while (matcher.find()) {
      MatchResult result = matcher.toMatchResult();
      System.out.println(result.group(1));
    }