Here you can find the source of getHrefInnerHtml(String href)
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";
Parameter | Description |
---|---|
href | a parameter |
public static String getHrefInnerHtml(String href)
//package com.java2s; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { /**// w ww.j av a2s .c om * 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; } /** * is null or its length is 0 * * <pre> * isEmpty(null) = true; * isEmpty("") = true; * isEmpty(" ") = false; * </pre> * * @param str * @return if string is null or its size is 0, return true, else return false. */ public static boolean isEmpty(String str) { return (str == null || str.length() == 0); } }