fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. String s = "My thing 0.02\nMy thing 100.2\nMy thing 65\nMy thing\n"+
  15. "0.03\nMy thing\n13\nMy thing\n 45.67 stuff\n";
  16. Matcher m = Pattern.compile("(My thing)\\s*(\\d+(?:\\.\\d+)?)").matcher(s);
  17. while (m.find()) {
  18. // Add to dictionary, group 1 is key, 2 is value
  19. System.out.println("Found: " + m.group(0)+ ":" + m.group(1)+":" + m.group(2));
  20. }
  21. }
  22. }
Success #stdin #stdout 0.09s 320576KB
stdin
Standard input is empty
stdout
Found: My thing 0.02:My thing:0.02
Found: My thing 100.2:My thing:100.2
Found: My thing 65:My thing:65
Found: My thing
0.03:My thing:0.03
Found: My thing
13:My thing:13
Found: My thing
    45.67:My thing:45.67