fork(1) download
  1. import java.io.*;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. class Ideone
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. String regex = "(\\w+\\s+\\+\\s+\\w+\\s+-->\\s+\\w+|\\w+\\s+-->\\s+\\w+\\s+\\+\\s+\\w+)[\\s\\n\\r]+(\\w+\\s+\\+\\s+\\w+\\s+-->\\s+\\w+|\\w+\\s+-->\\s+\\w+\\s+\\+\\s+\\w+)";
  10. String string = "a + b --> c\n\n"
  11. + "c + d --> e\n"
  12. + "The empty line after this is left alone\n\n"
  13. + "e + a --> b\n\n"
  14. + "a --> b + c";
  15. String subst = "$1\\n$2";
  16.  
  17. Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
  18. Matcher matcher = pattern.matcher(string);
  19. String result = matcher.replaceAll(subst);
  20.  
  21. System.out.println(result);
  22. }
  23. }
Success #stdin #stdout 0.08s 28004KB
stdin
Standard input is empty
stdout
a + b --> c\nc + d --> e
The empty line after this is left alone

e + a --> b\na --> b + c