JAva正規表現htmlラベルのプロパティの変更
package com.ez;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ZYJ {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// String htmlStr = "<b><font www=\"red\" pp='oo'>aaaaa</font><font www=\"red\" pp='oo'>aaaaa</font></b> ";
String htmlStr = "<b><a href='http://www.baby.com' pp='oo'/><a href =\"http://www.baby.com\" pp=\"o\">welcome</a></b> ";
System.out.println("zyj "+ZYJ.updateHtmlTag(htmlStr, "a", "href","http://www.redirect.com/xxx?url=" , "?xx=ss\""));
}
/**
* @param htmlStr html
* @param searchTag
* @param searchAttrib
* @param newStr
*/
public static String updateHtmlTag(String htmlStr, String searchTag,
String searchAttrib,String startStr , String endStr) {
String regxpForTag ="<\\s*" + searchTag + "\\s+([^>]*)\\s*>";
String regxpForTagAttrib = searchAttrib + "\\s*=\\s*[\"|']http://([^\"|']+)[\"|']";//"=[\"|']([^[\"|']]+)[\"|']";
Pattern patternForTag = Pattern.compile(regxpForTag);
Pattern patternForAttrib = Pattern.compile(regxpForTagAttrib);
Matcher matcherForTag = patternForTag.matcher(htmlStr);
StringBuffer sb = new StringBuffer();
boolean result = matcherForTag.find();
while (result) {
StringBuffer sbreplace = new StringBuffer("<"+searchTag +" ");
System.out.println(matcherForTag.group(1));
Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag
.group(1));
if (matcherForAttrib.find()) {
System.out.println("ll"+matcherForAttrib.group(1));
matcherForAttrib.appendReplacement(sbreplace, searchAttrib+"=\"" +startStr
+ matcherForAttrib.group(1) + endStr);
}
// matcherForTag.appendReplacement(sb, sbreplace.toString());
matcherForAttrib.appendTail(sbreplace);
matcherForTag.appendReplacement(sb, sbreplace.toString()+">");
result = matcherForTag.find();
}
matcherForTag.appendTail(sb);
return sb.toString();
}
}
参照先:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
*Title:HTML関連の正規表現ツールクラス
*
*
*Description:HTMLタグのフィルタリング、HTMLタグの変換、特定のHTMLタグの置換を含む
*
*
* Copyright: Copyright (c) 2006
*
*
* @author hejian
* @version 1.0
* @createtime 2006-10-16
*/
public class HtmlRegexpUtil {
private final static String regxpForHtml = "<([^>]*)>";//<先頭>>で終わるすべてのラベルをフィルタ
private final static String regxpForImgTag = "<\\s*img\\s+([^>]*)\\s*>";//IMGタグを見つける
private final static String regxpForImaTagSrcAttrib=“src=”([^"]+)”;//IMGタグのSRC属性を探し出す
/**
*
*/
public HtmlRegexpUtil() {
//TODO Auto-generated constructor stub
}
/**
*
*基本機能:タグを置き換えて正常に表示
*
*
* @param input
* @return String
*/
public String replaceTag(String input) {
if (!hasSpecialChars(input)) {
return input;
}
StringBuffer filtered = new StringBuffer(input.length());
char c;
for (int i = 0; i <= input.length() - 1; i++) {
c = input.charAt(i);
switch (c) {
case '<':
filtered.append("<");
break;
case '>':
filtered.append(">");
break;
case '"':
filtered.append(""");
break;
case '&':
filtered.append("&");
break;
default:
filtered.append(c);
}
}
return (filtered.toString());
}
/**
*
*基本機能:タグが存在するか否かを判断する
*
*
* @param input
* @return boolean
*/
public boolean hasSpecialChars(String input) {
boolean flag = false;
if ((input != null) && (input.length() > 0)) {
char c;
for (int i = 0; i <= input.length() - 1; i++) {
c = input.charAt(i);
switch (c) {
case '>':
flag = true;
break;
case '<':
flag = true;
break;
case '"':
flag = true;
break;
case '&':
flag = true;
break;
}
}
}
return flag;
}
/**
*
*基本機能:「<」で始まる「>」で終わるすべてのラベルをフィルタします.
*
*
* @param str
* @return String
*/
public static String filterHtml(String str) {
Pattern pattern = Pattern.compile(regxpForHtml);
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
boolean result1 = matcher.find();
while (result1) {
matcher.appendReplacement(sb, "");
result1 = matcher.find();
}
matcher.appendTail(sb);
return sb.toString();
}
/**
*
*基本機能:指定ラベルのフィルタリング
*
*
* @param str
* @param tag
*ラベルの指定
* @return String
*/
public static String fiterHtmlTag(String str, String tag) {
String regxp = "<\\s*"+ tag + "\\s+([^>]*)\\s*>";
Pattern pattern = Pattern.compile(regxp);
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
boolean result1 = matcher.find();
while (result1) {
matcher.appendReplacement(sb, "");
result1 = matcher.find();
}
matcher.appendTail(sb);
return sb.toString();
}
/**
*
*基本機能:指定したラベルを置換
*
*
* @param str
* @param beforeTag
*置換するラベル
* @param tagAttrib
*置換するラベル属性値
* @param startTag
*新規タグ開始フラグ
* @param endTag
*新規ラベル終了フラグ
* @return String
*@例えば、imgタグを置換するsrc属性値は[属性値]である[/img]
*/
public static String replaceHtmlTag(String str, String beforeTag,
String tagAttrib, String startTag, String endTag) {
String regxpForTag = "<\\s*"+ beforeTag + "\\s+([^>]*)\\s*>";
String regxpForTagAttrib = tagAttrib + "=\"([^\"]+)\"";
Pattern patternForTag = Pattern.compile(regxpForTag);
Pattern patternForAttrib = Pattern.compile(regxpForTagAttrib);
Matcher matcherForTag = patternForTag.matcher(str);
StringBuffer sb = new StringBuffer();
boolean result = matcherForTag.find();
while (result) {
StringBuffer sbreplace = new StringBuffer();
Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag
.group(1));
if (matcherForAttrib.find()) {
matcherForAttrib.appendReplacement(sbreplace, startTag
+ matcherForAttrib.group(1) + endTag);
}
matcherForTag.appendReplacement(sb, sbreplace.toString());
result = matcherForTag.find();
}
matcherForTag.appendTail(sb);
return sb.toString();
}
}