首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

JAVA代码取 匹配的 字符串

2013-12-04 
JAVA代码取 匹配的 字符串 在线等本帖最后由 beginI 于 2013-12-02 11:50:55 编辑div classnews_img_bo

JAVA代码取 匹配的 字符串 在线等
本帖最后由 beginI 于 2013-12-02 11:50:55 编辑

<div class='news_img_box'>
<div class='news_img_image' onclick="imgfun(32961292,'http://file.youboy.com/d/158/56/8/7/489817s.jpg')">
<img onload=changeImg(this,94,94,0) title='20110315(002).jpg' src='http://file.youboy.com/d/158/56/8/7/489817s.jpg' ></div>
<div class='news_img_hover' id='img_hover_32961292' >&nbsp;</div><div class='news_img_name'>20110315(002).jpg</div></div><div class='news_img_box'>
<div class='news_img_image' onclick="imgfun(32961291,'http://file.youboy.com/d/158/56/8/0/556180.JPG')">
<img onload=changeImg(this,94,94,0) title='100_4157.JPG' src='http://file.youboy.com/d/158/56/8/0/556180.JPG' ></div>
<div class='news_img_hover' id='img_hover_32961291' >&nbsp;</div>
<div class='news_img_name'>100_4157.JPG</div></div>
<div class='news_img_box'><div class='news_img_image' onclick="imgfun(32961287,'http://file.youboy.com/d/158/56/8/3/757893s.jpg')">
<img onload=changeImg(this,94,94,0) title='2PY5LLQU63@]V[_(I_)3SEP.jpg' src='http://file.youboy.com/d/158/56/8/3/757893s.jpg' ></div>
<div class='news_img_hover' id='img_hover_32961287' >&nbsp;</div><div class='news_img_name'>2PY5LLQU63@]V[_(I_)3SEP.jpg</div></div>
<div class='news_img_box'><div class='news_img_image' onclick="imgfun(32957816,'http://file.youboy.com/d/158/56/8/6/499406s.jpg')">
<img onload=changeImg(this,94,94,0) title='照片 017.jpg' src='http://file.youboy.com/d/158/56/8/6/499406s.jpg' ></div>
<div class='news_img_hover' id='img_hover_32957816' >&nbsp;</div>
<div class='news_img_name'>照片 017.jpg</div></div><div class='news_img_box'><div class='news_img_image' onclick="imgfun(32956832,'http://file.youboy.com/d/158/56/8/6/343906s.jpg')"><img onload=changeImg(this,94,94,0) title='IMAG0890.jpg' src='http://file.youboy.com/d/158/56/8/6/343906s.jpg' ></div>
<div class='news_img_hover' id='img_hover_32956832' >&nbsp;</div><div class='news_img_name'>IMAG0890.jpg</div></div><div class='s_pageDiv'>
<div id='cutpage'>当前页: 1</div><div id='pageCount' >共1页 </div></div>


JAVA代码获得 这段 html字符串,我需要通过JAVA代码取出 字符串中 所有的 图片src
分条保存为数组

我晓得有个 正则匹配 然后循环 的,但不知道怎么写了。。

在线等,,谢谢
[解决办法]
String s = "<img onload=changeImg(this,94,94,0) title='20110315(002).jpg' src='http://file.youboy.com/d/158/56/8/7/489817s.jpg'></div>";

Pattern p = Pattern.compile("src=\'(.*)\'");
Matcher m = p.matcher(s);
if (m.find()) {
System.out.println("xxx:" + m.group(1));
}

[解决办法]
2楼那个正则已经能匹配出src='http://XXXXXX'了啊 
难道你只要那个地址链接?'http://XXXXXX'?
如果是只要地址链接最多再做个字符串替换的处理,把"src="替换成空串"",
imgList.add(m.group(1).replaceAll("src=",""));

或者做一下字符串截取不就完事了?
[解决办法]

String str="<div class='news_img_box'>" +
"<div class='news_img_image' onclick="imgfun(32961292,'http://file.youboy.com/d/158/56/8/7/489817s.jpg')">" +
"<img onload=changeImg(this,94,94,0) title='20110315(002).jpg' src='http://file.youboy.com/d/158/56/8/7/489817s.jpg' ></div>" +
"<div class='news_img_hover' id='img_hover_32961292' > </div><div class='news_img_name'>20110315(002).jpg</div></div><div class='news_img_box'>" +
"<div class='news_img_image' onclick="imgfun(32961291,'http://file.youboy.com/d/158/56/8/0/556180.JPG')">" +
"<img onload=changeImg(this,94,94,0) title='100_4157.JPG' src='http://file.youboy.com/d/158/56/8/0/556180.JPG' ></div>" +
"<div class='news_img_hover' id='img_hover_32961291' > </div>" +
"<div class='news_img_name'>100_4157.JPG</div></div>" +
"<div class='news_img_box'><div class='news_img_image' onclick="imgfun(32961287,'http://file.youboy.com/d/158/56/8/3/757893s.jpg')">" +
"<img onload=changeImg(this,94,94,0) title='2PY5LLQU63@]V[_(I_)3SEP.jpg' src='http://file.youboy.com/d/158/56/8/3/757893s.jpg' ></div>" +


"<div class='news_img_hover' id='img_hover_32961287' > </div><div class='news_img_name'>2PY5LLQU63@]V[_(I_)3SEP.jpg</div></div>" +
"<div class='news_img_box'><div class='news_img_image' onclick="imgfun(32957816,'http://file.youboy.com/d/158/56/8/6/499406s.jpg')">" +
"<img onload=changeImg(this,94,94,0) title='照片 017.jpg' src='http://file.youboy.com/d/158/56/8/6/499406s.jpg' ></div>" +
"<div class='news_img_hover' id='img_hover_32957816' > </div>" +
"<div class='news_img_name'>照片 017.jpg</div></div><div class='news_img_box'>" +
"<div class='news_img_image' onclick="imgfun(32956832,'http://file.youboy.com/d/158/56/8/6/343906s.jpg')">" +
"<img onload=changeImg(this,94,94,0) title='IMAG0890.jpg' src='http://file.youboy.com/d/158/56/8/6/343906s.jpg' ></div>" +
"<div class='news_img_hover' id='img_hover_32956832' > </div><div class='news_img_name'>IMAG0890.jpg</div></div>" +
"<div class='s_pageDiv'>" +
"<div id='cutpage'>当前页: 1</div><div id='pageCount' >共1页 </div></div>";
Matcher m=Pattern.compile("(?s)(?<=<img).*?src='(.*?)'").matcher(str);
while(m.find())
System.out.println(m.group(1));

热点排行