fork(2) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.HashMap;
  4. import java.util.LinkedHashMap;
  5. import java.util.Map;
  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 inputText = "state: UP flag: 9 time: 02:29:09.111782 highest_id: num_users: 0 num_records: 0";
  15. Map<String, String> pairs = new LinkedHashMap<>(); //using Linked so they stay in order
  16. Matcher m = Pattern.compile("(\\S+):\\s(\\S*)(?:\\b(?!:)|$)").matcher(inputText);
  17. while (m.find()) {
  18. pairs.put(m.group(1), m.group(2));
  19. }
  20. for (Map.Entry entry : pairs.entrySet()) {
  21. System.out.println(entry.getKey() + ": " + entry.getValue());
  22. }
  23. }
  24. }
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
state: UP
flag: 9
time: 02:29:09.111782
highest_id: 
num_users: 0
num_records: 0