Java tutorial
//package com.java2s; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { /** * get innerHtml from href * * <pre> * getHrefInnerHtml(null) = "" * getHrefInnerHtml("") = "" * getHrefInnerHtml("mp3") = "mp3"; * getHrefInnerHtml("<a innerHtml</a>") = "<a innerHtml</a>"; * getHrefInnerHtml("<a>innerHtml</a>") = "innerHtml"; * getHrefInnerHtml("<a<a>innerHtml</a>") = "innerHtml"; * getHrefInnerHtml("<a href="baidu.com">innerHtml</a>") = "innerHtml"; * getHrefInnerHtml("<a href="baidu.com" title="baidu">innerHtml</a>") = "innerHtml"; * getHrefInnerHtml(" <a>innerHtml</a> ") = "innerHtml"; * getHrefInnerHtml("<a>innerHtml</a></a>") = "innerHtml"; * getHrefInnerHtml("jack<a>innerHtml</a></a>") = "innerHtml"; * getHrefInnerHtml("<a>innerHtml1</a><a>innerHtml2</a>") = "innerHtml2"; * </pre> * * @param href * @return <ul> * <li>if href is null, return ""</li> * <li>if not match regx, return source</li> * <li>return the last string that match regx</li> * </ul> */ public static String getHrefInnerHtml(String href) { if (isEmpty(href)) { return ""; } String hrefReg = ".*<[\\s]*a[\\s]*.*>(.+?)<[\\s]*/a[\\s]*>.*"; Pattern hrefPattern = Pattern.compile(hrefReg, Pattern.CASE_INSENSITIVE); Matcher hrefMatcher = hrefPattern.matcher(href); if (hrefMatcher.matches()) { return hrefMatcher.group(1); } return href; } public static boolean isEmpty(String str) { if (str == null) return true; if ("null".equals(str)) return true; if ("".equals(str.trim())) return true; return false; } }