1. 程式人生 > >正則表示式替換img標籤src值

正則表示式替換img標籤src值



package com.test;

import java.util.HashSet;

import java.util.Set;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

publicclass ImgFilter {

publicstaticvoid main(String[] args) {

Set<String> pics = new HashSet<>();

String htmlStr="fdagdffsgfdf <img width=\"100%\" src=\"/documents/38006/0/coupons7.png/b740c61d-348d-e4f1-e8d5-d2b0a9aea93e?t=1504851883150\" />"

+ "sfdgfdggfhhg<b>hhsydsfdsyuudgggggggggggg";

//filter img tag

String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";

Pattern p_image = Pattern.compile(regEx_img,Pattern.CASE_INSENSITIVE);

Matcher m_image = p_image.matcher(htmlStr);

intimgStartIndex=0;

intimgEndIndex=0;

StringBuffer

sbreplace = new StringBuffer();

while(m_image.find()){

imgStartIndex=m_image.start();

imgEndIndex=m_image.end();

//filter src value in img

Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(m_image.group());

while(m.find()){

//replace src value in img

m.appendReplacement(

sbreplace, "src=''");

pics.add(m.group(1));

}

//append last img tag content

m.appendTail(sbreplace);

}

// before img tag add replaced img content and add last img tag content

System.out.println(htmlStr.substring(0, imgStartIndex)+sbreplace.toString()+htmlStr.substring(imgEndIndex+1, htmlStr.length()));

}

}