fork download
  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  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 regex = "^(?:/[a-z0-9_-]+)+";
  13. String string = "/news\n"
  14. + "/news?param1=value1\n"
  15. + "/news#anchor?param1=value1";
  16.  
  17. Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
  18. Matcher matcher = pattern.matcher(string);
  19. String result = matcher.replaceAll("$0/");
  20.  
  21. System.out.println(result);
  22. }
  23. }
Success #stdin #stdout 0.08s 33796KB
stdin
Standard input is empty
stdout
/news/
/news/?param1=value1
/news/#anchor?param1=value1