AmyWuGo
AmyWuGo

Reputation: 2425

Regular expression how to replace the src link in img tag

I tried many ways to do this. And I am totally new to Regular expression. I want to replace all img src link to other link.

My html file just are like this:

<img src="01"></img><img src="02"></img><img src="03"></img>

or it would be like this:

<  img src  =  "01"></img><    img src="02"><    img src = "03"></img>

There might be space or just without "</img>"

and I want them be like this way:

<div><p><DIV class="a"><img src="01"></img></p></div><div><p><DIV class="a"><img src="02"></img></p></div><div><p><DIV class="a"><img src="03"></img></p></div>

and I use this to get the img src link:

            Pattern p = null;
            Matcher m = null;
            p = Pattern.compile("<img[^>]*src\\s*=\\s*\"([^\"]*)");
            m = p.matcher(mystr);
            while (m.find()) {
                imgIDList.add(m.group(1));
            }

and I made the str list to replace: ArrayList imgList4Replace = new ArrayList();

and I use this to excuse replace :

                mystr.replace(("<img[^>]*src\\s*=\\s*\""+imgListReplaceOriginal.get(nIndex)+"([^\"]*)"), imgList4Replace.get(nIndex)+"$2");

it just don't work. I've spent so much time to test.

And need your help. Thank you very much.

Upvotes: 1

Views: 7805

Answers (3)

Jignesh Vachhani
Jignesh Vachhani

Reputation: 35

Here is the code :

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class ImgTest {
    public static void main(String[] args) {

            String s = "This is a sample<img src=\"test.html\" /> text";
            Pattern p = Pattern.compile("[<](/)?img[^>]*[>]");
            Matcher m = p.matcher(s);
            if (m.find()) {
              String src = m.group();
              System.out.println(src);
            }
            s = s.replaceAll("[<](/)?img[^>]*[>]", "");
            System.out.println(s);
    }
}

Upvotes: 3

cubanacan
cubanacan

Reputation: 654

Here you are:

private static String replaceSrcs(String str, List<String> srcs) {
    Pattern p = Pattern.compile("(<\\s*img\\s*src\\s*=\\s*\").*?(\"\\s*>)");
    Matcher m = p.matcher(str);
    StringBuffer sb = new StringBuffer();
    int i = 0;
    while (m.find()) {
        m.appendReplacement(sb, "$1" + srcs.get(i++) + "$2");
    }
    m.appendTail(sb);
    return sb.toString();
}

Now you need just invoke it:

replaceSrcs(mystr, imgList4Replace);

And it returns what you like.

Upvotes: 1

Brian Agnew
Brian Agnew

Reputation: 272417

You can't reliably use regexps with HTML/XML. You need an HTML parser, such as the confusingly named JTidy (although it claims to be an HTML pretty-printer, it also gives you a DOM-view on your document)

Upvotes: 5

Related Questions