fork download
  1. /* http://es.stackoverflow.com/q/51051/127 */
  2.  
  3. import java.util.regex.Matcher;
  4.  
  5. class Ideone
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. String ruta = "C:\\Archive\\carpeta\\archivo.ej";
  10. String regex = "^C:\\\\Archive(?:\\\\+|$)";
  11. String reemp = "E:\\";
  12. System.out.println("Ruta: " + ruta);
  13.  
  14. ruta = ruta.replaceFirst(regex, Matcher.quoteReplacement(reemp));
  15. //escapar caracteres especiales ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  16. System.out.println("Reemplazada: " + ruta);
  17. }
  18. }
Success #stdin #stdout 0.05s 4386816KB
stdin
Standard input is empty
stdout
Ruta: C:\Archive\carpeta\archivo.ej
Reemplazada: E:\carpeta\archivo.ej