当前位置: 代码迷 >> Java相关 >> 求正则表达式!解决思路
  详细解决方案

求正则表达式!解决思路

热度:5834   发布时间:2013-02-25 21:44:18.0
求正则表达式!!
比如字符串:
HTML code
<p><img title="img02.jpg" style="float:none;width:292px;height:173px"  src="/skycms/uploads/1/images/20120920/2012092016465574123.jpg" width="163" border="0" /><img style="width:204px;height:196px" height="340" src="http://www.baidu.com/1.jpg" width="520" /></p><p style="line-height:16px"><img src="/skycms/ueditor/dialogs/attachment/fileTypeImages/icon_rar.gif" /><a href="/skycms/uploads/1/soft/20120920/2012092016473380473.zip">附件.zip</a></p><p> </p>


我是用编辑器上传的,这是内容,我想解析得到img图片的src路径,和<a>标签里面的href的路径,其中要求是图片[b][/b]和附件(a标签)[b][/b]得到的都是以/skycms开头的路径,排除掉其他的别的开头的路径。哪位大侠会啊???



------解决方案--------------------------------------------------------
[src|href]="(/skycms/[^"]+)"

这样应该可以吧
------解决方案--------------------------------------------------------
Java code
    public static void main(String[] args) {        String s = "<img title=\"img02.jpg\" style=\"float:none;width:292px;height:173px\"  src=\"images/20120920/2012092016465574123.jpg\" />";        String s1 = "><a href=\"/skycms/uploads/1/soft/20120920/2012092016473380473.zip\">";        Matcher m = Pattern.compile("<(?i)img\\s+.*?(?i)src\\s*=\\s*\"(.+?)\".+?>").matcher(s);        while(m.find()){            System.out.println(m.group(1));        }        m = Pattern.compile("<(?i)a\\s+.*?(?i)href\\s*=\\s*\"(.+?)\".*?>").matcher(s1);        while(m.find()){            System.out.println(m.group(1));        }    }