There is such a line

<div class="emet_index" data-placeholder="current">138,05</div> 

How to find and display numbers from this line? in any way. Of course the best is not difficult. This line is not repeated.

  • Clarify the question. From this line or the line can be another? How different? Or should the numbers be pulled out of an arbitrary div tag? Need a string ( String ) or a number ( double )? - user194374

3 answers 3

If you take this line from HTML, then JSOUP (or another parser) will help you. Find the desired item and take its contents using the text() method

And do not get fooled by the advice of those who suggest using regular expressions to solve this problem.

Updated

Example:

 import org.jsoup.Jsoup; import org.jsoup.nodes.*; class Test { public static void main(String[] args) throws Exception { // Загружаем в парсер страницу с указанного URL (есть методы, // которыми можно взять страницу из переменной или файла) Document doc = Jsoup.connect("http://site.ru/page").get(); // Выбираем нужные элементы согласно CSS-селектору (".emet_index"), // берем их текст и выводим в консоль System.out.print(doc.select(".emet_index").text()); } } 

JSOUP API is very similar to jQuery

  • And through jsoup an example on this issue is possible? really tired of looking. Print the whole page, I mastered it, but the search in it is already problematic. I read the documentation and was puzzled. - DevilScream
  • @DevilScream It seems to be nothing complicated. Completed the answer with an example. First, the page is loaded (from the network or from the file), then the necessary elements are selected using the select() method, the argument of which is the CSS selector, then the text content is taken from the element. - tutankhamun
  • Are you a cool dude can your contact be? mail / wk / skype / very rescued. Realistically. I shook so much in shock and here the lines of the line / you're cool / - DevilScream
  • @DevilScream examples even on this podsite can be searched by jsoup tag - tutankhamun

As option through simple SubString

  String myStr = "<div class=\"emet_index\" data-placeholder=\"current\">138,05</div>"; String sec = myStr.substring(myStr.indexOf(">") + 1, myStr.indexOf("</")); System.out.println(sec); 

    I would use replaceAll and a regular expression

     String s = "<div class=\"emet_index\" data-placeholder=\"current\">138,05</div>"; String str = s.replaceAll("[^0-9]", ""); 

    If you need a comma - it is also not difficult to modify.

    • No need to use regulars when parsing HTML! - Vartlok
    • Perhaps because of his inexperience, he advised this method, but I am sure that in some cases it can be applied. Although when testing api, I confess, parsil json-simple - abbath0767
    • one
      RegExp is undoubtedly a useful thing and you need to know them and be able to use them. BUT! For parsing HTML this is completely unsuitable. It is too complex to take into account all the nuances. - Vartlok