String line = "<p><img width='365' height='480' alt='' src='1299891849786.jpg' /></p>" +
"<p><img width='365' height='480' alt='' src='1299891849786.jpg' /></p>";我要把它变成
String line = "<p><img width='365' height='480' alt='inline' src='1299891849786.jpg' /></p>" +
"<p><img width='365' height='480' alt='inline' src='1299891849786.jpg' /></p>";
也就是说我要在img标签中增加 alt='inline',同时,要把img标签里面的src的路径放到一个数组里面去。(注意img标签外的alt 不能做处理,而且之外的src也不需取出来)
我该如何去处理这个问题,大家帮帮忙呀
"<p><img width='365' height='480' alt='' src='1299891849786.jpg' /></p>";我要把它变成
String line = "<p><img width='365' height='480' alt='inline' src='1299891849786.jpg' /></p>" +
"<p><img width='365' height='480' alt='inline' src='1299891849786.jpg' /></p>";
也就是说我要在img标签中增加 alt='inline',同时,要把img标签里面的src的路径放到一个数组里面去。(注意img标签外的alt 不能做处理,而且之外的src也不需取出来)
我该如何去处理这个问题,大家帮帮忙呀
解决方案 »
- 程序中会出现java.lang.NullPointerException的错误
- 空格在java的控制台输入中究竟扮演着什么样的角色?
- Swing:怎样在一个面板里按下按钮,对另一个面板重绘?
- 求助如何使用Pattern类和Matcher类中的方法查找特定的匹配,并返回匹配到的内容
- if...else...的问题
- 有关Java的时间问题
- java下连mysql出错(linux系统)
- 求 jdk 1.5 windows 32bit release 版
- 各位大哥,请帮个忙!!!
- 关于正则表达式和json解析的问题
- 异常a different object with the same identifier value was already associated with
- Java数据库操作大全
String line = "<p><img width='365' height='480' alt='' src='1299891849786.jpg' /></p>" +
"<p><img width='365' height='480' alt='' src='1299891849786.jpg' /></p>";Document doc = Jsoup.parseBodyFragment(line);
Elements els = doc.body().select("img");//1
els.attr("alt","inline");//2
ArrayList<String> imagepathlist = new ArrayList<String>();
for(Element e:els){
imagepathlist.add(e.attr("src"));
}
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class T {
public static void main(String[] args) {
String line = "<p><img width='365' height='480' alt='' src='1299891849786.jpg' /></p>"
+ "<p><img width='365' height='480' alt='' src='1299891849786.jpg' /></p>";
//替换alt=''为alt='inline'
line =line.replaceAll("(<img.*?)(alt='')(.*?/>)", "$1 alt='inline' $3");
//找到img中的src中的值,存入src
ArrayList<String> src = new ArrayList<String>();
Pattern pattern = Pattern.compile("<img.*?src=\\'(.*?)\\'.*?/>");
Matcher matcher = pattern.matcher(line);
while(matcher.find()) {
String temp = matcher.group();
int len = "src='".length();
int start = temp.indexOf("src='");
int end = temp.indexOf("'", start+len);
src.add(temp.substring(start+len,end));
}
System.out.println(line);
System.out.println(src);
}
}
字符串替换 alt='' alt='inline'剩下取出src的 可以用字符串截取 先按空格split 再按=号split如果你熟悉正则表达式 可以用正则提取~
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;public class Example {
public static void main(String[] args) {
String line = "<p><img width='365' height='480' alt='' src='1299891849786.jpg' /></p>" +
"<p><img width='365' height='480' alt='' src='1299891849786.jpg' /></p>";
String regex = "alt=''";
System.out.println(line.replaceAll(regex, "alt=inline"));
List<String> list = new ArrayList<String>();
String regex3 = "\\s";
String[] str1 = line.split(regex3);
for(int i=0; i<str1.length; i++) {
if(str1[i].indexOf("src=")!=-1) {
String strr[] = str1[i].split("src=");
if(strr.length == 2) {
list.add(strr[1]);
}
}
}
System.out.println(list);
}}
1、如果line 里面的<img> 标签中本身就没有alt=''标签,那我怎么给这个标签加入alt='inline'
2、<img>标签里面的SRC图片分为本地图片和网图片,我现在只想对本地图片做处理,那我该怎么办呢??
Document doc = Jsoup.parseBodyFragment(line);
Elements els = doc.body().select("img");这两个java类到底是属于哪个?我不知道选择哪个。而且我没看到有Elements 只看到了Element
String line = "<p><img width='365' height='480' alt='' src='1299891849786.jpg' /></p>" +
"<p><img width='365' height='480' alt='' src='1299891849786.jpg' /></p>";
String new_line = new String();
new_line = line.replaceAll("alt=''", "alt='inline'");
System.out.println(new_line);
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class T {
public static void main(String[] args) throws SocketException {
String line = "<p><img width='365' height='480' alt='' src='1299891849786.jpg' /></p>"
+ "<p><img width='365' height='480' src='http://www.baidu.com/1299891849786.jpg' /></p>";
//替换alt=''为alt='inline'
line =line.replaceAll("(<img.*?)(alt='')*?(.*?/>)", "$1 alt='inline' $3");
//找到img中的src中的值,存入src
ArrayList<String> src = new ArrayList<String>();
Pattern pattern = Pattern.compile("<img.*?src=\\'(.*?)\\'.*?/>");
Matcher matcher = pattern.matcher(line);
while(matcher.find()) {
String temp = matcher.group();
if(!isLocal(temp))
continue;
int len = "src='".length();
int start = temp.indexOf("src='");
int end = temp.indexOf("'", start+len);
src.add(temp.substring(start+len,end));
}
System.out.println(line);
System.out.println(src);
}
/**
* 用来判断img中src属性中的文件是否为本地的
* @param path
* @return
* @throws SocketException
*/
private static boolean isLocal(String path) throws SocketException {
StringBuffer header = new StringBuffer();
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while(en.hasMoreElements()) {
NetworkInterface ni = en.nextElement();
Enumeration<InetAddress> enAddr = ni.getInetAddresses();
while(enAddr.hasMoreElements()) {
header.append("src=\'http:/" + enAddr.nextElement());
header.append("|");
}
}
header.append("src=\'http://localhost");
//header用来判断是否为src="http://127.0.0.1或src="http://192.168.1.235”或src="http://localhost”
boolean local = false;
//用正则表达式和字符串的相关函数进行判断
Pattern p = Pattern.compile(header.toString());
Matcher m = p.matcher(path);
if(m.find()) {//匹配是否为http开头的本地文件
local = true;
} else if(path.indexOf("src='http://") != -1) {//网络文件
local = false;
} else {//否则就是本地文件
local = true;
}
return local;
}
}这个解决了你上面说的两个问题,应该能满足你的要求了吧。
import java.util.regex.*;
public class TestString { /**
* @param args
*/
static String[] test(String str){
String p1="<img[^>]+alt=";
String _p1="<img[^>]+alt='[a-zA-Z]*'";
Pattern one= Pattern.compile(p1),two=Pattern.compile(_p1);
Matcher matcher1=one.matcher(str),matcher2=two.matcher(str);
int array_time=0;
while(matcher1.find()&&matcher2.find()){
++array_time;
str=str.replace(matcher2.group(),matcher1.group()+"'inline'" );
}
System.out.println(str);
String p2="<img[^>]+.jpg";
one=Pattern.compile(p2);
String[] array=new String[array_time];
array_time=0;
Matcher matcher3=one.matcher(str);
while(matcher3.find()){
array[array_time++]=matcher3.group().substring(matcher3.group().indexOf("src='")+5);
}
return array;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String line = "<p><img width='365' height='480' alt='' src='1299891849786.jpg' /></p>" +
"<p><img width='365' height='480' alt='' src='1299891849786.jpg' /></p>";
String[] array=test(line);
for(String s:array)
System.out.println(s);
}}