fork(1) 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.*;
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String s = "dummy:10.45someObjectdummy:10.67somethingdummy:10.78somethingdummy:10.98djd";
  13. Matcher m = Pattern.compile("dummy:(\\d+\\.\\d+)").matcher(s);
  14. List<Double> vals = new ArrayList<>();
  15. while (m.find()){
  16. vals.add(Double.parseDouble(m.group(1)));
  17. }
  18. int val_id = vals.size() / 2 - 1;
  19. Double updated_val = vals.get(val_id);
  20. System.out.println("We need to update " + updated_val);
  21. updated_val = updated_val + 1;
  22. String replace_pattern = "(dummy:(?:\\d+\\.\\d.*?dummy:){" + (val_id) + "})\\d+\\.\\d+";
  23. System.out.println("New pattern: " + replace_pattern);
  24. String result = s.replaceFirst(replace_pattern, "$1" + updated_val);
  25. System.out.println(result);
  26.  
  27. }
  28. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
We need to update 10.67
New pattern: (dummy:(?:\d+\.\d.*?dummy:){1})\d+\.\d+
dummy:10.45someObjectdummy:11.67somethingdummy:10.78somethingdummy:10.98djd