, these numbers change. How should I fit these? ...">

Match Jsoup with regex

I ran into a problem using jsoup. I cannot match <div id="shout_132684"> , these numbers change. How should I fit these?

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

Does not work.

+4
source share
2 answers

You can use the initial CSS selector ^= . It is supported by Jsoups .select(...) .

You can do it as follows:

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

This is a complete example:

 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); } } 

He will print:

 <div id="shout_23"></div> <div id="shout_42"></div> <div id="shout_1337"></div> 
+11
source

For more precise parsing, you can still do this with regular expressions:

 Elements content = doc.select("div[id~=(shout_)[0-9]+]"); 
+5
source

Source: https://habr.com/ru/post/1501148/


All Articles