Search code examples
javaftpftp-clientftp-server

Java: Regular Expression for FTP server list?


I'm writing FTP client handler in Java and I can't use FTP libraries like Apache.

My problem is that I receive the list from the server in this format:

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

While all I need is the file/directory name and size.

Please suggest me a way to reduce the list to names and sizes, keeping in mind spacing differences between the columns and spacing in the filenames.

Thank you all in advance :)


Solution

  • Since you just want to grab the data, you can use this regex:

    "(?m)^.{20}\\s*(\\d+).{14}(.*)$"
    

    And construct a Pattern, and obtain a Matcher corresponding to the input string and start extracting matches. The size can be obtained in group(1) and the file name can be obtained in group(2).