正则表达式去除html标签
发布时间:2020-12-13 23:09:04 所属栏目:百科 来源:网络整理
导读:/** * 删除Html标签 * * @param inputString * @return */public static String htmlRemoveTag(String inputString) { if (inputString == null) return null; String htmlStr = inputString; // 含html标签的字符串 String textStr = ""; java.util.regex.P
/** * 删除Html标签 * * @param inputString * @return */ public static String htmlRemoveTag(String inputString) { if (inputString == null) return null; String htmlStr = inputString; // 含html标签的字符串 String textStr = ""; java.util.regex.Pattern p_script; java.util.regex.Matcher m_script; java.util.regex.Pattern p_style; java.util.regex.Matcher m_style; java.util.regex.Pattern p_html; java.util.regex.Matcher m_html; try { //定义script的正则表达式{或<script[^>]*?>[sS]*?</script> String regEx_script = "<[s]*?script[^>]*?>[sS]*?<[s]*?/[s]*?script[s]*?>"; //定义style的正则表达式{或<style[^>]*?>[sS]*?</style> String regEx_style = "<[s]*?style[^>]*?>[sS]*?<[s]*?/[s]*?style[s]*?>"; String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式 p_script = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE); m_script = p_script.matcher(htmlStr); htmlStr = m_script.replaceAll(""); // 过滤script标签 p_style = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE); m_style = p_style.matcher(htmlStr); htmlStr = m_style.replaceAll(""); // 过滤style标签 p_html = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE); m_html = p_html.matcher(htmlStr); htmlStr = m_html.replaceAll(""); // 过滤html标签 textStr = htmlStr; } catch (Exception e) { e.printStackTrace(); } return textStr;// 返回文本字符串 } public static void requestPost(String ipString) throws IOException{ /** * 首先要和URL下的URLConnection对话。 URLConnection可以很容易的从URL得到。比如: // Using * java.net.URL and //java.net.URLConnection * * 使用页面发送请求的正常流程:在页面http://www.faircanton.com/message/loginlytebox. * asp中输入用户名和密码,然后按登录, * 跳转到页面http://www.faircanton.com/message/check.asp进行验证 验证的的结果返回到另一个页面 * * 使用java程序发送请求的流程:使用URLConnection向http://www.faircanton.com/message/ * check.asp发送请求 并传递两个参数:用户名和密码 然后用程序获取验证结果 */ URL url = new URL("http://199604.com/ip/"); URLConnection connection = url.openConnection(); /** * 然后把连接设为输出模式。URLConnection通常作为输入来使用,比如下载一个Web页。 * 通过把URLConnection设为输出,你可以把数据向你个Web页传送。下面是如何做: */ connection.setDoOutput(true); /** * 最后,为了得到OutputStream,简单起见,把它约束在Writer并且放入POST信息中,例如: ... */ OutputStreamWriter out = new OutputStreamWriter( connection.getOutputStream(),"8859_1"); out.write("ip=" + ipString + "&action=2"); // 向页面传递数据。post的关键所在! // out.write("username=kevin&password=*********"); //向页面传递数据。post的关键所在! // remember to clean up out.flush(); out.close(); /** * 这样就可以发送一个看起来象这样的POST: POST /jobsearch/jobsearch.cgi HTTP 1.0 ACCEPT: * text/plain Content-type: application/x-www-form-urlencoded * Content-length: 99 username=bob password=someword */ // 一旦发送成功,用以下方法就可以得到服务器的回应: String sCurrentLine; String sTotalString; sCurrentLine = ""; sTotalString = ""; InputStream l_urlStream; l_urlStream = connection.getInputStream();//获取返回的Html内容 // 传说中的三层包装阿! BufferedReader l_reader = new BufferedReader(new InputStreamReader( l_urlStream)); System.out.println("第一次过滤"); while ((sCurrentLine = l_reader.readLine()) != null) { sTotalString+= sCurrentLine; } String regEx_script = "<script[^>]*?>.*?</script>"; sTotalString = sTotalString.replaceAll(regEx_script,""); String regEx_style = "<style[^>]*?>.*?</style>"; sTotalString = sTotalString.replaceAll(regEx_style,""); String html_regex = "<(.[^>]*)>";// /过滤标签的规则 Pattern p = Pattern.compile(html_regex);// 将规则封装成对象 BufferedReader htmlbufr = new BufferedReader(new StringReader(sTotalString)); BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("E://IpHTML.txt"))); while ((sCurrentLine = htmlbufr.readLine()) != null) { sCurrentLine = sCurrentLine.replaceAll(html_regex,""); bufw.write(sCurrentLine); bufw.newLine();// /换行 bufw.flush();// 刷新 } bufw.close(); System.out.println("第一次过滤完毕,开始下一轮过滤"); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |