java中如何用正则表达式判断这样的东西?

比如要用java代码对文本化的html进行处理 要将img元素的height 和 width都去掉
比如有一段文本"<img src=\"1.jpg\" width=\"665\" height=\"533\"/>"
我现在要通过程序去掉width=\"665\" height=\"533\" 怎么处理?
因为要涉及处理很多img标签 每个img标签的属性值都不一样

String source = "<img src=\"1.jpg\" width=\"665\" height=\"533\"/>";

// 这个方法看起来比较简便 但是依赖属性顺序
source = source.replaceAll("(<img.*?)(?:\\s(?:width|height)=\".*?\")+(.*?>)","$1 $2");

// 这个方法能够应对更多的情况
//source = source.replaceAll("(<img.*?)width=\".*?\"(.*?>)","$1$2").replaceAll("(<img.*?)height=\".*?\"(.*?>)","$1$2");

System.out.println(source);

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-06-18
Pattern p = Pattern.compile("width=\\\"\\d{3}\\\" height=\\\"\\d{3}\\\"");
Matcher m = p.matcher(s); //其中s是你要判断的文本
String str = null;
while(m.find()) {
str = m.replaceAll("");
}
//str是改后的文本