O
O
ostrovskiy_andrey2015-04-16 21:22:21
Java
ostrovskiy_andrey, 2015-04-16 21:22:21

How to parse text or number by mask into Android Studio variable?

There is a line: <div class="blockk">Free memory: 29568 b.<br><hr>
I want to get a variable with a value of 29568 b
Using a type mask <div class="blockk">Free memory: (.+?).<br><hr>
I don't know how to implement this. I read about regular expressions, in all examples the comparison and the result is false or true, but how to pull out the value you need, I did not find examples, tell me where to read?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
asd111, 2015-04-17
@ostrovskiy_andrey

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

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

      // Строка, которую будем парсить
      String line = "<div class=\\"blockk\\">Free memory: 29568 b.<br><hr>";

       // Наша регулярка
      String pattern = "<div class=\\"blockk\\">Free memory: (.+?).<br><hr>";

      Pattern r = Pattern.compile(pattern);
      Matcher m = r.matcher(line);

      // Если нашли то выводим
      while (m.find( )) {
         System.out.println("Found value: " + m.group(0) );
      } else {
         System.out.println("NO MATCH");
      }
   }
}

A
anyd3v, 2015-04-17
@anyd3v

Read about regular expressions, indulge in some online tester for example https://regex101.com/ , then how to use regular expressions in java. Your regex is not correct.
If I understand correctly, it will
Free memory: ([^\.]+).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question