你在Java代码里怎么加啊;
加了也没超链啊,
你要放在什么地方,
说具体点儿
加了也没超链啊,
你要放在什么地方,
说具体点儿
解决方案 »
- java.lang.reflect.InvocationTargetException
- 请问java如何判断一个文件类型
- 求助啊,svn+maven项目启动报错,Error on line 1 of document : The processing instruction ta
- java jdk版本问题
- java http下载,产生connection或者io异常该怎么处理?让它跳转错误页面.
- 关于“String空”类型转换
- 网站整合
- 哪些原因会导致show_sql和直接运行SQL结果不一样?
- 初学Struts!
- 请教: gson 爆出declares multiple JSON fields named的解决方法
- JSP中圖片顯示的問題
- Hibernate3怎么读取 Boolean属性
就是说要把那个字符串里面的"http://www.cimc.com"的取出来.
用正则表达式
static String regex ="((http://)([a-z]+[.])|(www.))\\w+[.]([a-z]{2,4})+((/[\\S&&[^,;\u4E00-\u9FA5]]+)+)?([.][a-z]{2,4}+|/?)";
static String A1 ="<a href={0} target='_blank'>";
static String A2 ="</a>";
/**
* 给发布的消息加超链接
*
* @param msg
* @return
*/
public static String checkMsg(String msg)
{
StringBuilder sb = new StringBuilder(msg);
Pattern pat =Pattern.compile(regex);
Matcher mat = pat.matcher(msg);
int index = 0;
int index1 = 0;
while (mat.find())
{
String url = mat.group();
if (url.indexOf("http://") != 0)
url = "http://"+url;
Object obj[] ={"'"+url+"'"};
String a = MessageFormat.format(A1, obj);
int l = a.length();
index += index1;
sb.insert(mat.start()+index, a);
index += l;
sb.insert((mat.end())+index, A2);
index1 = A2.length();
}
return sb.toString();
}
String str = "深圳中集专用车有限公司隶属于十年绩优蓝筹股——"
+ "中国国际海运集装箱(集团)http://www.cimc.com 股份有限公司,是中集车辆(集团)"
+ "有限公司的全资下属企业,是中集车辆集团在华南地区" + "的半挂车和改装车产业的大型生产基地。 ";
你可试试把上面的字符串传入进去.
深圳中集专用车有限公司隶属于十年绩优蓝筹股——中国国际海运集装箱(集团)<a href='http://www.cimc.com'>http://www.cimc.com</a> 股份有限公司,是中集车辆(集团)有限公司的全资下属企业,是中集车辆集团在华南地区的半挂车和改装车产业的大型生产基地。
public static void main(String args[])
{
String str1 = "深圳中集专用车有限公司隶属于十年绩优蓝筹股——"
+ "中国国际海运集装箱(集团)http://www.cimc.com 股份有限公司,是中集车辆(集团)"
+ "有限公司的全资下属企业,是中集车辆集团在华南地区" + "的半挂车和改装车产业的大型生产基地。 ";
String [] str ={
str1
};
for (String s:str)
{
//System.out.format("%-50s%b\n", s,parse(s));
System.out.println(s+"<br/>");
//parse(s);
System.out.println(parse(s));
//System.out.println("---------------------------------");
}
}
//(http:)?(//)?([a-z]+[.])?\\w+[.]\\w+(/|[.][a-z]+)
//(http:)?(//)?([a-z]+[.])?\\w+[.]([a-z])+
//(http://)?(//)?([a-z]+[.])?\\w+[.]([a-z])+((/\\w+)+)?([.][a-z]+|/?)
//((http://)([a-z]+[.])|(www.))\\w+[.]([a-z])+((/\\w+)+)?([.][a-z]{1}+|/?)
//((http://)([a-z]+[.])|(www.))\\w+[.]([a-z]{2,4})+((/\\w+)+)?([.][a-z]{2,4}+|/?)
static String regex ="((http://)([a-z]+[.])|(www.))\\w+[.]([a-z]{2,4})+((/[\\S&&[^,;\u4E00-\u9FA5]]+)+)?([.][a-z]{2,4}+|/?)";
static String A1 ="<a href={0}>";
static String A2 ="</a>";
static String parse(String title)
{
StringBuilder sb = new StringBuilder(title);
Pattern pat =Pattern.compile(regex);
Matcher mat = pat.matcher(title);
int index = 0;
int index1 = 0;
while (mat.find())
{
String url = mat.group();
System.out.println(url);
if (url.indexOf("http://") != 0)
url = "http://"+url;
Object obj[] ={"'"+url+"'"};
String a = MessageFormat.format(A1, obj);
int l = a.length();
index += index1;
sb.insert(mat.start()+index, a);
index += l;
sb.insert((mat.end())+index, A2);
index1 = A2.length();
}
return sb.toString();
}
深圳中集专用车有限公司隶属于十年绩优蓝筹股——中国国际海运集装箱(集团)http://www.cimc.com 股份有限公司,是中集车辆(集团)有限公司的全资下属企业,是中集车辆集团在华南地区的半挂车和改装车产业的大型生产基地。 晕.一样的代码我的结果输出怎么不是一样的?
import java.util.regex.Pattern;
import java.text.MessageFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;这个不会错的