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.*;
  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 input = "Passage : Hi David! how are you? are you in chennai now? "
  13. + "\n Hi Ram! how are you?\n are you in chennai now?";
  14. String regex1="Hi\\s+(.*?)\\s+how\\s+are\\s+you\\?\\s+are\\s+you\\s+in\\s+(.*?)\\s+now\\?";
  15. Pattern p = Pattern.compile(regex1,Pattern.DOTALL);
  16. Matcher m = p.matcher(input);
  17.  
  18. StringBuffer result = new StringBuffer();
  19. while (m.find()) {
  20. m.appendReplacement(result,m.group(1)+" is fine and yes I am in "+m.group(2));
  21. }
  22. m.appendTail(result);
  23. System.out.println(result);
  24. }
  25. }
Success #stdin #stdout 0.11s 320576KB
stdin
Standard input is empty
stdout
Passage : David! is fine and yes I am in chennai 
 Ram! is fine and yes I am in chennai