二手车贩子年收入多少:怎样在java中去除网页的" "

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/01 21:05:50
利用htmlparser提取出网页的内容
但用replaceAll(" ","")并不能用空字符串替换掉" "。
是将 & n b s p;
用replaceAll("& n b s p;","")不适用
网上有人说replace只能替换单字符
%20是什么意思?trim()只是去头尾的空格

1.去除单个HTML标记
String s="asdfasd<script>asdfsfd</script>1234";
System.out.println(s.replaceAll("<script.*?(?<=/script>)",""));
2.去除所有HTML标记
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HTMLSpirit{
public static String delHTMLTag(String htmlStr){
String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式

Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
Matcher m_script=p_script.matcher(htmlStr);
htmlStr=m_script.replaceAll(""); //过滤script标签

Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
Matcher m_style=p_style.matcher(htmlStr);
htmlStr=m_style.replaceAll(""); //过滤style标签

Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
Matcher m_html=p_html.matcher(htmlStr);
htmlStr=m_html.replaceAll(""); //过滤html标签

return htmlStr.trim(); //返回文本字符串
}
}

那就
replaceAll(" ","")
必须是处理源代码..^_^

 

replaceAll("[\\s]*","");

试试%20

java不是有一个trim()的方法吗,可以去除空格的呀