fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.regex.*;
  5. class Ideone
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. Map<String, String> dictionary = new HashMap<String, String>();
  10. dictionary.put("ä", "ae");
  11. dictionary.put("ö", "oe");
  12. dictionary.put("ü", "ue");
  13. String s = "<h1 id=\"anwendungsfälle-und--funktionen\">Anwendungsfälle und -funktionen</h1> \n<h1 id=\"öl\">Öl</h1>";
  14. StringBuffer result = new StringBuffer();
  15. Matcher m = Pattern.compile("(\\G(?!^)|<h\\d+\\s+id=\")([^\"]*?)([üöä])").matcher(s);
  16. while (m.find()) {
  17. m.appendReplacement(result, m.group(1) + m.group(2) + dictionary.get(m.group(3)));
  18. }
  19. m.appendTail(result);
  20. System.out.println(result.toString());
  21.  
  22. }
  23. }
Success #stdin #stdout 0.06s 27876KB
stdin
Standard input is empty
stdout
<h1 id="anwendungsfaelle-und--funktionen">Anwendungsfälle und -funktionen</h1> 
<h1 id="oel">Öl</h1>