fork 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.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 prep = "{b} this {/b} is \\{b} a **fully** ***featured * test** *of* __my__ ~~thing~~" +
  15. "~~yes__it~~is__";
  16. String out = convertFormatting(prep);
  17. System.out.println(out);
  18. System.out.println("the previous line is supposed to look like this:");
  19. System.out.println("\\{b} this \\{/b} is \\\\{b} a {b}fully{/b} " +
  20. "{b}{i}featured {/i} test{/b} {i}of{/i} {u}my{/u} {s}thing{/s}" +
  21. "{s}yes{u}it{/s}is{/u}");
  22. // your code goes here
  23. }
  24.  
  25. public static String convertFormatting(String input) {
  26. String output;
  27. Pattern bold = Pattern.compile("(?<!\\\\)\\*\\*(([^*]|\\*(?!\\*))+)?\\*\\*");
  28. Pattern underline = Pattern.compile("(?<!\\\\)__(([^_]|_(?!_))+)?__");
  29. Pattern italics = Pattern.compile("(?<!\\\\)\\*([^\\*]+?)\\*|_([^_]+?)_");//DO NOT RUN THIS ONE BEFORE RUNNING BOTH BOLD AND UNDERLINE
  30. Pattern strike = Pattern.compile("(?<!\\\\)~~(([^~]|~(?!~))+)?~~");
  31. Pattern prep = Pattern.compile("(\\{([bius]|\\/[bius])})");
  32.  
  33. System.out.println(input);
  34. Matcher m = prep.matcher(input);
  35. if(m.find()) {
  36. input = m.replaceAll("\\\\$1");
  37. }
  38. System.out.println(input);
  39. m = bold.matcher(input);
  40. if(m.find()) {
  41. input = m.replaceAll("{b}$1{/b}");
  42. }
  43. System.out.println(input);
  44.  
  45. m = underline.matcher(input);
  46. if(m.find()) {
  47. input = m.replaceAll("{u}$1{/u}");
  48. }
  49. System.out.println(input);
  50. m = strike.matcher(input);
  51. if(m.find()) {
  52. input = m.replaceAll("{s}$1{/s}");
  53. }
  54. System.out.println(input);
  55. m = italics.matcher(input);
  56. if(m.find()) {
  57. input = m.replaceAll("{i}$1{/i}");
  58. }
  59. System.out.println(input);
  60.  
  61. output = input;//comment this out before you push an update, smartass
  62. return output;
  63. }
  64. }
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
{b} this {/b} is \{b} a **fully** ***featured * test** *of* __my__ ~~thing~~~~yes__it~~is__
\{b} this \{/b} is \\{b} a **fully** ***featured * test** *of* __my__ ~~thing~~~~yes__it~~is__
\{b} this \{/b} is \\{b} a {b}fully{/b} {b}*featured * test{/b} *of* __my__ ~~thing~~~~yes__it~~is__
\{b} this \{/b} is \\{b} a {b}fully{/b} {b}*featured * test{/b} *of* {u}my{/u} ~~thing~~~~yes{u}it~~is{/u}
\{b} this \{/b} is \\{b} a {b}fully{/b} {b}*featured * test{/b} *of* {u}my{/u} {s}thing{/s}{s}yes{u}it{/s}is{/u}
\{b} this \{/b} is \\{b} a {b}fully{/b} {b}{i}featured {/i} test{/b} {i}of{/i} {u}my{/u} {s}thing{/s}{s}yes{u}it{/s}is{/u}
\{b} this \{/b} is \\{b} a {b}fully{/b} {b}{i}featured {/i} test{/b} {i}of{/i} {u}my{/u} {s}thing{/s}{s}yes{u}it{/s}is{/u}
the previous line is supposed to look like this:
\{b} this \{/b} is \\{b} a {b}fully{/b} {b}{i}featured {/i} test{/b} {i}of{/i} {u}my{/u} {s}thing{/s}{s}yes{u}it{/s}is{/u}