實例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public static String repDomain(String source, String domain, String element, String attr) { String img = "" ; Pattern p_image; Matcher m_image; String regEx_img = "<" + element + "[^<>]*?\\s" + attr + "=['\"]?(.*?)['\"]?(\\s.*?)?>" ; p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE); m_image = p_image.matcher(source); while (m_image.find()) { img = m_image.group(); Matcher m = Pattern.compile( "href\\s*=\\s*\"?(.*?)(\"|>|\\s+)" ).matcher(img); while (m.find()) { String srcVal = m.group( 1 ); if (srcVal.indexOf( "/@tenant" )>= 0 ){ int idx = srcVal.indexOf( "/@tenant" ); StringBuffer temp = new StringBuffer(); String dstVal = temp.append(domain).append(srcVal.substring(idx+ 1 )).toString(); source = source.replace(srcVal, dstVal); } } } return source; } |
以上就是小編為大家?guī)淼膉ava正則表達式獲取指定HTML標簽的指定屬性值且替換的方法全部內(nèi)容了,希望大家多多支持服務(wù)器之家~