求一个获取html页面当中 <style type=”text/css”></style>和<style type=””text/css””></style> , 附上文本: <html xmlns=" http://www.w3.org/1999/xhtml"><head> <meta content="text/html; charset=utf-8" http-equiv="content-type"/> <meta name="keywords" content="测试"/> <meta name="description" content="测试"/> <title>测试</title> <script src="http://my.com/b.js" charset="utf-8" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="http://my.com/c.css"/> <link rel="stylesheet" href="http://my.com/d.css"/> <script type="text/javascript" src="/js/local_comm.js"></script> <link rel="stylesheet" href="/css/remote_base.css"/> <style type="text/css"> .c{text-link:none} </style> <link rel="stylesheet" href="/css/remote_custom_css.css"/> <style type=""text/css""> @charset "utf-8";.q .hd{background:none; background-image : url(http://my.com/line.gif );background-repeat : no-repeat ;}</style></head> <body> <div name="script"><script type="text/javascript"> alert("ok"); </script> <script language="javascript" type="text/javascript"> alert("ok"); </script> </div><div id="_script"><script charset="UTF-8" type="text/javascript" src="http://my.com/b.js"></script></div></body></html> 匹配出来的结果为: <style type=””text/css””> @charset “utf-8″;.q .hd{background:none; background-image : url(http://my.com/line.gif );background-repeat : no-repeat ;}</style> <script type=”text/javascript”> 和 同时子表达式可以获取他们的内容. 最好写两个正则表达式 一个获取style标签的 一个获取script标签的。 |
|
35分 |
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { //"<style\s+type=["""]text/css["""]>([^<]*)</style>" //"<script\s+(?:language="javascript"\s+)?type=["""]text/javascript["""]>([^<]*)</script>" /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String str = "<style type="text/css">.c{text-link:none}</style>"; Pattern p = Pattern.compile("<[stylecrip]{5,6}\s+(?:language="javascript"\s+)?type=["""]text/[javascript]{3,10}["""]>([^<]*)</[stylecrip]{5,6}>"); Matcher m = p.matcher(str); while(m.find()){ System.out.println(m.group()); System.out.println(m.group(1)); } } } |
5分 |
楼上 的试试看看吧。
|