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 rx = "(?i)w(?:e{2})?k(\\d+)(?:(?:\\s*until\\s*|[ _\\/])(?:w(?:e{2})?k)?(\\d+))?";
  13. String s = "Soccer International wk43\nNational Geopgraphic (wk50)\nSchoolpaper wk39/wk43\nSome magazine week12 until 16\nAnother magazine wk36_38\nAnother magazine wk36_wk38";
  14. StringBuffer result = new StringBuffer();
  15. Matcher m = Pattern.compile(rx).matcher(s);
  16. while (m.find()) {
  17. String replacement = m.group(2) == null ?
  18. "week " + m.group(1):
  19. "week " + m.group(1) + " - week " + m.group(2);
  20. m.appendReplacement(result, replacement);
  21. }
  22. m.appendTail(result);
  23. System.out.println(result.toString());
  24.  
  25. }
  26. }
Success #stdin #stdout 0.11s 320576KB
stdin
Standard input is empty
stdout
Soccer International week 43
National Geopgraphic (week 50)
Schoolpaper week 39 - week 43
Some magazine week 12 - week 16
Another magazine week 36 - week 38
Another magazine week 36 - week 38