java正则表达式图⽚地址_使⽤Java正则表达式匹配出⽹页图
⽚地址并替换掉
⾸先说明⼀下.我是做的邮件发送功能..~~图⽚已经上传到了服务器,现在的问题是,发送后的图⽚不能正常显⽰,路径不对..我是想⽤Java正则表达式匹配出上传的图⽚地址并替换掉
⽅法⼀:
publicclassTestImg2 {
privateString replaceImgSrc(String content,String replaceHttp){
String result ="";
//String patternStr="^.*.*$";
//String patternStr=".*?.*";
//String patternStr="^.*.*$";
//String patternStr="]+[gif|jpg|jpeg|bmp|bmp]*)(\"{1}|\'{1})(?:.*)>";
//String patternStr="]*?>src=\"?(.*?)(\"|>|\\s+)";
//String patternStr="(?i)]*?src=\"([])\"";
//String patternStr = "]*?src="((\\w+?:\\/\\/|/)[^"]*?)"[^>]*?>";
//        String patternStr = "]*?src=\"((\\w+?:?//|\\/|\\w*)[^\"]*?)\"[^>]*?>";
String patternStr = "]*?src=[\"|\']((\\w+?:?//|\\/|\\w*)[^\"]*?)[\"|\'][^>]*?>";
Pattern pattern=Patternpile(patternStr);
Matcher matcher = pattern.matcher(content);
//如果匹配到了img
System.out.println("matcher.matches() == "+matcher.matches());
if(matcher.matches()){
up(1),(up(1)));
System.out.println(" result == "+result);
}else{
result =content;
}
returnresult;
}
publicstaticvoidmain(String[] args) {
TestImg2 ss =newTestImg2();
replaceall()String content ="
//String content = "
" ;
}
}
⽅法⼆
publicclassTestImg {
publicstaticvoidmain(String[] args) {
String content ="
";
//String content = "
//content = placeAll("(.*)src=\"(.*)", "$1src=\"127.0.0.1:8088$2");
content = placeAll("(.*?)src=\"(.*?)","$1src=\"127.0.0.1:8088$2");
/
/String contents =placeAll("(.*?)src=\"(.*?)","$1src=\""+path+"$2");
System.out.println(content);
//String str = ""; }
}
//获取项⽬的基路径(⽤于拼接图⽚的全路径)
privateString basePaths(){
FacesContext context = CurrentInstance();
ExternalContext ec = ExternalContext();
HttpServletRequest request = (HttpServletRequest) ec.getRequest();
String basePath = Scheme()+"://"+ServerName()+":"+ServerPort();
// System.out.println("RequestURL() == "+RequestURL());
System.out.println("basePath == "+basePath);
returnbasePath;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。