原创作品。转载请注明出处https://blog.youkuaiyun.com/kk123k/article/details/81003722
获取到一个<a>标签的时候,Java如何利用正则表达式判断里面是否含有title属性呢?如果含有title属性,则获取其title属性的值,如果没有title属性,则获取其内容
判断是否含有title属性的正则表达式为
<a.*title\s*=\s*["'](.*)["'].*>.*</a>
在Java字符串中,必须加上转义字符\
String regex = "<a.*title\\s*=\\s*[\"'](.*)[\"'].*>.*</a>";
完整测试代码
public static void main(String[] args) {
String regex = "<a.*title\\s*=\\s*[\"'](.*)[\"'].*>.*</a>";
String test1 = "<a href=\"#this\" title =\"我有title属性\">我也有内容</a>";
String test2 = "<a href=\"#this\" >我没有title =\"这是我内容而已\"</a>";
String hasT1 = "<a.*title\\s*=\\s*[\"']";
String hasT2 = "[\"'].*>.*</a>";
String noT1 = "<a.*?>";
String noT2 = "</a>";
boolean isMatch = Pattern.matches(regex, test1);
System.out.println(test1 + " " + isMatch);//<a href="#this" title ="我有title属性">我也有内容</a> true
isMatch = Pattern.matches(regex, test2);
System.out.println(test2 + " " + isMatch);//<a href="#this" >我没有title ="这是我内容而已"</a> false
//test1有title属性,获取其title值
Pattern p1 = Pattern.compile(hasT1);
Matcher m1 = p1.matcher(test1);
String result1 = m1.replaceAll("");
p1 = Pattern.compile(hasT2);
m1 = p1.matcher(result1);
result1 = m1.replaceAll("");
System.out.println(result1);//我有title属性
//test2没有title属性,获取其内容
Pattern p2 = Pattern.compile(noT1);
Matcher m2 = p2.matcher(test2);
String result2 = m2.replaceAll("");
p2 = Pattern.compile(noT2);
m2 = p2.matcher(result2);
result2 = m2.replaceAll("");
System.out.println(result2);//我没有title ="这是我内容而已"
}
输出结果如下: