fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. /* Name of the class has to be "Main" only if the class is public. */
  4. class Ideone
  5. {
  6. public static void main (String[] args) {
  7. String example = "_Text_\n" +
  8. "*text*\n" +
  9. "~Text~\n" +
  10. "`Text`\n" +
  11. "_Text_\n" + // is it only matching the first one?
  12. "``` Text ```\n" +
  13. "\\(Text\\)\n" +
  14. "~Text~\n";
  15. System.out.println(process(example));
  16. }
  17.  
  18. private static String process(String input) {
  19. String processed = input.replaceAll("\\b_[^_]+_\\b", "underscore")
  20. .replaceAll("\\B\\*[^*]+\\*\\B", "star")
  21. .replaceAll("\\B```.+?```\\B", "backticks")
  22. .replaceAll("\\B~[^~]+~\\B", "tilde")
  23. .replaceAll("\\B`[^`]+`\\B", "tick")
  24. .replaceAll("\\B\\\\\\(.*?\\\\\\)\\B", "backslashparen");
  25.  
  26. return processed;
  27. }
  28.  
  29. }
Success #stdin #stdout 0.08s 33888KB
stdin
Standard input is empty
stdout
underscore
star
tilde
tick
underscore
backticks
backslashparen
tilde