There was a problem to remake the FTP links to the file to a more "user-friendly" view.

Now links look like:

ftp://ftp.domain.com:22/path/to/the/file 

I want to convert them under this form:

 \\ftp\ftp\path\to\the\file 

This is done so that the user can, by copying this link, go through the Explorer to this file and download / open it. I write this method of change in Java, I decided using regular expressions to find and change values, but some test cases are not covered.

The method itself:

 public String reformatLink(String filePath) { return filePath .replaceAll("ftp://", "//") .replaceAll("((.domain.com(:[\\w]+)*)|(:[\\w]+))", "/ftp") .replaceAll("/", "\\\\"); } 

Junit test:

 @Parameterized.Parameters public static Collection<Object[]> sources() { return Arrays.asList(new Object[][]{ {"ftp://ftp.domain.com:22/Temp_Users_Data/test/test.zip"}, {"ftp://ftp:22/Temp_Users_Data/test/test.zip"}, {"ftp://ftp/Temp_Users_Data/test/test.zip"}, {"ftp://ftp.domain.com/Temp_Users_Data/test/test.zip"}}); } @org.junit.Test public void test() { String result = source .replaceAll("ftp://", "//") .replaceAll("((.domain.com(:[\\w]+)*)|(:[\\w]+))", "/ftp") .replaceAll("/", "\\\\"); assertEquals("\\\\ftp\\ftp\\Temp_Users_Data\\test\\test.zip", result); } 

The value ftp://ftp/Temp_Users_Data/test/test.zip does not fit this condition. Maybe you should somehow remake the regulars? Who faced a similar problem, tell me, please.

    1 answer 1

     String[] links = {"ftp://ftp.domain.com:22/Temp_Users_Data/test/test.zip", "ftp://ftp:22/Temp_Users_Data/test/test.zip", "ftp://ftp/Temp_Users_Data/test/test.zip", "ftp://ftp.domain.com/Temp_Users_Data/test/test.zip"}; for (String s : links) { s = "\\\\" + s.replaceFirst("ftp://", "").replaceFirst("([.:][^/]+)?/", "/ftp/").replace('/', '\\'); System.out.println(s); } 

    Prints:

     \\ftp\ftp\Temp_Users_Data\test\test.zip \\ftp\ftp\Temp_Users_Data\test\test.zip \\ftp\ftp\Temp_Users_Data\test\test.zip \\ftp\ftp\Temp_Users_Data\test\test.zip 
    • It helped, thank you very much - alex