从FTP LIST命令解析响应(语法变化)

时间:2021-10-24 03:44:30

The FTP LIST command displays a listing of all the files and directories in the current working directory. The problem is, it returns several different formats depending on the server. Does anybody know of a .NET library that is able to parse the most popular formats? I am OK with a "try this regex, if it fails, try the next regex" approach.

FTP LIST命令显示当前工作目录中所有文件和目录的列表。问题是,它根据服务器返回几种不同的格式。有没有人知道能够解析最流行格式的.NET库?我可以用“试试这个正则表达式,如果它失败了,试试下一个正则表达式”的方法。

4 个解决方案

#1


Here's the one that I've been using for a FileZilla server:

这是我一直用于FileZilla服务器的那个:

^(?<dir>[\-ld])(?<permission>([\-r][\-w][\-xs]){3})\s+(?<filecode>\d+)\s+(?<owner>\w+)\s+(?<group>\w+)\s+(?<size>\d+)\s+(?<timestamp>((?<month>\w{3})\s+(?<day>\d{2})\s+(?<hour>\d{1,2}):(?<minute>\d{2}))|((?<month>\w{3})\s+(?<day>\d{1,2})\s+(?<year>\d{4})))\s+(?<name>.+)$

http://chrishaas.wordpress.com/2009/06/10/regex-for-parsing-ftp-list-command/

#2


Here is a RegEx I used on a project. Seems to work for both Windows and Unix based FTP servers. Someone might be able to clean it up but I build it by concatenating a bunch of properties on a class. So it's not as brutal to maintain for me.

这是我在项目中使用的RegEx。似乎适用于基于Windows和Unix的FTP服务器。有人可能能够清理它,但我通过在类上连接一堆属性来构建它。所以对我来说并不是那么残酷。

^((?<DIR>([dD]{1}))|)(?<ATTRIBS>(.*))\s(?<SIZE>([0-9]{1,}))\s(?<DATE>((?<MONTHDAY>((?<MONTH>(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\s(?<DAY>([0-9\s]{2}))))\s(\s(?<YEAR>([0-9]{4}))|(?<TIME>([0-9]{2}\:[0-9]{2})))))\s(?<NAME>([A-Za-z0-9\-\._\s]{1,}))$

#3


^(?<dir>[\-ld])(?<permission>([\-r][\-w][\-xs]){3})\s+(?<filecode>\d+)\s+(?<owner>\w+)\s+(?<group>\w+)\s+(?<size>\d+)\s+(?<timestamp>((?<month>\w{3})\s+(?<day>\d{1,2})\s+(?<hour>\d{1,2}):(?<minute>\d{2}))|((?<month>\w{3})\s+(?<day>\d{1,2})\s+(?<year>\d{4})))\s+(?<name>.+)$

Changed Chris Haas version a tiny little bit. Changed so that the day grouping can consist of a single number as well. \d{2} -> \d{1,2}

改变克里斯哈斯版本一点点。已更改,以便日期分组也可以包含一个数字。 \ d {2} - > \ d {1,2}

Thanks for original version.

谢谢原版。

#4


^(?<dir>[\-ld])(?<permission>([\-r][\-w][\-xs]){3})\s+(?<filecode>\d+)\s+(?<owner>\S+)\s+(?<group>\S+)\s+(?<size>\d+)\s+(?<timestamp>((?<month>\w{3})\s+(?<day>\d{1,2})\s+(?<hour>\d{1,2}):(?<minute>\d{2}))|((?<month>\w{3})\s+(?<day>\d{1,2})\s+(?<year>\d{4})))\s+(?<name>.+)$

On the site I'm on, the owner is displayed in Email address format. I changed owner and group to be non-space characters instead of word characters.

在我所在的网站上,所有者以电子邮件地址格式显示。我将所有者和组更改为非空格字符而不是单词字符。

This is expanding on Yodiz' version of Chris Haas' version. Thanks so much!

这在Yodiz版本的Chris Haas版本上有所扩展。非常感谢!

#1


Here's the one that I've been using for a FileZilla server:

这是我一直用于FileZilla服务器的那个:

^(?<dir>[\-ld])(?<permission>([\-r][\-w][\-xs]){3})\s+(?<filecode>\d+)\s+(?<owner>\w+)\s+(?<group>\w+)\s+(?<size>\d+)\s+(?<timestamp>((?<month>\w{3})\s+(?<day>\d{2})\s+(?<hour>\d{1,2}):(?<minute>\d{2}))|((?<month>\w{3})\s+(?<day>\d{1,2})\s+(?<year>\d{4})))\s+(?<name>.+)$

http://chrishaas.wordpress.com/2009/06/10/regex-for-parsing-ftp-list-command/

#2


Here is a RegEx I used on a project. Seems to work for both Windows and Unix based FTP servers. Someone might be able to clean it up but I build it by concatenating a bunch of properties on a class. So it's not as brutal to maintain for me.

这是我在项目中使用的RegEx。似乎适用于基于Windows和Unix的FTP服务器。有人可能能够清理它,但我通过在类上连接一堆属性来构建它。所以对我来说并不是那么残酷。

^((?<DIR>([dD]{1}))|)(?<ATTRIBS>(.*))\s(?<SIZE>([0-9]{1,}))\s(?<DATE>((?<MONTHDAY>((?<MONTH>(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\s(?<DAY>([0-9\s]{2}))))\s(\s(?<YEAR>([0-9]{4}))|(?<TIME>([0-9]{2}\:[0-9]{2})))))\s(?<NAME>([A-Za-z0-9\-\._\s]{1,}))$

#3


^(?<dir>[\-ld])(?<permission>([\-r][\-w][\-xs]){3})\s+(?<filecode>\d+)\s+(?<owner>\w+)\s+(?<group>\w+)\s+(?<size>\d+)\s+(?<timestamp>((?<month>\w{3})\s+(?<day>\d{1,2})\s+(?<hour>\d{1,2}):(?<minute>\d{2}))|((?<month>\w{3})\s+(?<day>\d{1,2})\s+(?<year>\d{4})))\s+(?<name>.+)$

Changed Chris Haas version a tiny little bit. Changed so that the day grouping can consist of a single number as well. \d{2} -> \d{1,2}

改变克里斯哈斯版本一点点。已更改,以便日期分组也可以包含一个数字。 \ d {2} - > \ d {1,2}

Thanks for original version.

谢谢原版。

#4


^(?<dir>[\-ld])(?<permission>([\-r][\-w][\-xs]){3})\s+(?<filecode>\d+)\s+(?<owner>\S+)\s+(?<group>\S+)\s+(?<size>\d+)\s+(?<timestamp>((?<month>\w{3})\s+(?<day>\d{1,2})\s+(?<hour>\d{1,2}):(?<minute>\d{2}))|((?<month>\w{3})\s+(?<day>\d{1,2})\s+(?<year>\d{4})))\s+(?<name>.+)$

On the site I'm on, the owner is displayed in Email address format. I changed owner and group to be non-space characters instead of word characters.

在我所在的网站上,所有者以电子邮件地址格式显示。我将所有者和组更改为非空格字符而不是单词字符。

This is expanding on Yodiz' version of Chris Haas' version. Thanks so much!

这在Yodiz版本的Chris Haas版本上有所扩展。非常感谢!