0

私は以下のcustomerTextを持っています

 String customerText="TestBody <img src="test.action&attachmentId=3313&custId=456 /> Sometext @attachmentId=3313";

attachmentId = 3313(画像タグ内にある)のすべての出現箇所をattachmentId=3824に置き換えたい。

したがって、上記の入力に対して期待される出力は次のとおりです。

出力は

 "TestBody <img src="test.action&attachmentId=3824&custId=456 /> Sometext @attachmentId=3313";
4

3 に答える 3

1

use this regex (?<=test\.action&attachmentId=)(\d+)(?=&|$|(/>)| ) to replace with 3824

于 2012-12-13T10:13:47.733 に答える
1

Even if in a particular case a regex solves your problem with HTML manipulation, regexs are not the proper tools for that work. HTML is not a regular language so you better don't use regex for those tasks. Use an HTML parser. You can achieve your goal pretty easily with Jsoup:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class MyJsoupExample2 {
    public static void main(String args[]) {
        String inputText = "<html><head></head><body><p><img src=\"test.action&attachmentId=3313&custId=456\" /></p>"
            + "<p>someText <img src=\"getCustomers.do?custCode=2&customerId=3340&param2=456\"/></p></body></html>";
        Document doc = Jsoup.parse(inputText);
        Elements myImgs = doc.select("img[src*=attachmentId=3313");
        for (Element element : myImgs) {
            String src = element.attr("src");
            element.attr("src", src.replace("attachmentId=3313", "attachmentId=3824"));
        }
        System.out.println(doc.toString());
    }
}

The code gets the list of img nodes with a src attribute containing your target string:

Elements myImgs = doc.select("img[src*=attachmentId=3313");

and loop over the list replacing the value of the src attribute with your desired value.

I know it is not as appealing as a one-line solution but believe me, it is much better than using a regex. You can find lots of threads on StackOverflow giving the same advice (including this one :).

于 2012-12-14T16:41:17.930 に答える
0

正規表現の使用に完全に取り掛かっていない場合、最も簡単な解決策は単純な文字列置換です。

String customerText="TestBody <img src=\"test.action&attachmentId=3313&custId=456\" />";
String output = customerText.replace("attachmentId=3313", "attachmentId=3824");
于 2012-12-13T10:27:45.850 に答える