fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.regex.*;
  5. /* Name of the class has to be "Main" only if the class is public. */
  6. class Ideone
  7. {
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. String text = "plan plans lander planitia<path id=\"DE\" data-id=\"\"/><path id=\"AU\" data-id=\"\" /> rest of the file";
  11. String pattern = "<path.*?(id=\"(.*?)\").*?(data-id=\"(.*?)\").*?>";
  12. Pattern r = Pattern.compile(pattern);
  13. Matcher m = r.matcher(text);
  14. while (m.find()) {
  15. System.out.println(m.group(1));
  16. System.out.println(m.group(2));
  17. String idReplacement = "";
  18. if (m.group(2).equals("DE")) {
  19. idReplacement = "Deutschland";
  20. } else {
  21. idReplacement = "AUSTRIA";
  22. }
  23. m.appendReplacement(sb, m.group(0).replaceFirst(Pattern.quote(m.group(1)), "id=\""+idReplacement+"\"").replaceFirst(Pattern.quote(m.group(3)), "data-id=\"DATAID\""));
  24. }
  25. m.appendTail(sb); // append the rest of the contents
  26. System.out.println(sb.toString());
  27. }
  28. }
Success #stdin #stdout 0.15s 36396KB
stdin
Standard input is empty
stdout
id="DE"
DE
id="AU"
AU
plan plans lander planitia<path id="Deutschland" data-id="DATAID"/><path id="AUSTRIA" data-id="DATAID" /> rest of the file