8

jsoup の使用中に問題が発生しました。<div id="shout_132684">それらの数字が変化していると一致できません。それらをどのように一致させる必要がありますか?

Elements content = doc.select("div:matches(id=\"shout_.+?\")");

うまくいきません。

4

2 に答える 2

14

startswith CSS セレクターを使用できます^=。Jsoups によってサポートされています.select(...)

次のように実行できます。

doc.select("div[id^=shout]");

これは完全な例です:

public static void main(String[] args)  {
    Document parse = Jsoup.parse("<div id=\"shout_23\"/>" +
                                 "<div id=\"shout_42\"/>" +
                                 "<div id=\"notValidId\"/>" +
                                 "<div id=\"shout_1337\"/>");

    Elements divs = parse.select("div[id^=shout");

    for (Element element : divs) {
        System.out.println(element);
    }
}

それは印刷されます:

<div id="shout_23"></div>
<div id="shout_42"></div>
<div id="shout_1337"></div>
于 2013-09-08T09:47:56.460 に答える
5

より正確な解析のために、正規表現でそれを行うことができます:

Elements content = doc.select("div[id~=(shout_)[0-9]+]");
于 2013-11-03T22:48:01.757 に答える