Search code examples
javaregexftpftp-client

Java. regex for parsing FTP server list


I am having troubles with my regex to filter FTP server list to file/directory name and size. This is the regex I'm currently using (?m)^.{20}\\s*(\\d+).{14}(.*)$
So, using this to filter this list

drw-rw-rw- 1 ftp ftp                0 Mar 17 06:10 Tor Browser
-rw-rw-rw- 1 ftp ftp          1538814 Jun 26 00:23 setup.exe
-rw-rw-rw- 1 ftp ftp           142570 May 24 05:28 satellite A665-S6086.pdf


I will get Tor Browser, 0 and setup.exe, 1538814
which is what I actually wants but in a list of this format

 -r-xr-xr-x  1 0  1001     4521014 May 23  2011 FileZilla_3.5.0_win32-setup.exenull
 dr-xr-xr-x  4 0  1001  1024 Mar  2 14:07 pubnull

I will get something like: ay 23 2011 FileZilla_3.5.0_win32-setup.exe, 1 and 14:07 pub ,1.
Is there a way i can make this regex works for both types?
Thank you


Solution

  • I would use the following:

    Pattern regex = Pattern.compile(
        "(\\d+)        # File size           \n" +
        "\\s+          # Whitespace          \n" +
        "\\w{3}        # Month (3 letters)   \n" +
        "\\s+          # Whitespace          \n" +
        "\\d{1,2}      # Day (1 or 2 digits) \n" +
        "\\s+          # Whitespace          \n" +
        "[\\d:]{4,5}   # Time or year        \n" +
        "\\s+          # Whitespace          \n" +
        "(.*)          # Filename            \n" +
        "$             # End of line", 
        Pattern.MULTILINE | Pattern.COMMENTS);