fork download
  1. /* ¿Como se obtienen los grupos de esta exprecion regular?
  2.   https://es.stackoverflow.com/q/71535/127 */
  3.  
  4. import java.util.*;
  5. import java.lang.*;
  6. import java.io.*;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9.  
  10.  
  11. class Ideone
  12. {
  13. private static void filtraNumeros(String texto) {
  14.  
  15. String regex = "(?<![\\d.])\\d{1,12}(?:\\.\\d{1,3})?+\\b";
  16.  
  17. Pattern patron = Pattern.compile(regex);
  18. Matcher matcher = patron.matcher(texto);
  19. int contador = 0;
  20.  
  21. //bucle para cada coincidencia.
  22. while (matcher.find()) {
  23. //Imprimir todo el texto con el que se coincidió
  24. System.out.println( ++contador + "° número: " + matcher.group() );
  25. }
  26. }
  27.  
  28. public static void main (String[] args) throws java.lang.Exception
  29. {
  30. // Prueba
  31. String prueba ="abc 123 def 12.345 -- demasiados decimales: 1.1234 987654321.321 0";
  32. filtraNumeros(prueba);
  33. }
  34. }
Success #stdin #stdout 0.07s 2841600KB
stdin
Standard input is empty
stdout
1° número: 123
2° número: 12.345
3° número: 987654321.321
4° número: 0