I have these strings as a response from a FTP server:
-
02-17-11 01:39PM <DIR> dec -
04-06-11 11:17AM <DIR> Feb 2011 -
05-10-11 07:09PM 87588 output.xlsx -
06-10-11 02:52PM 3462 output.xlsx
where the pattern is: [datetime] [length or <dir>] [filename]
Edit: my code was- @"^\d{2}-\d{2}-\d{2}(\s)+(<DIR>|(\d)+)+(\s)+(.*)+"
I need to parse these strings in this object:
class Files{
Datetime modifiedTime,
bool ifTrueThenFile,
string name
}
Please note that, filename may have spaces.
I am not good at regex matching, can you help?
Regex method
One approach is using this regex
I am capturing groups, so
Notice the syntax
(?:xx), it means that the content here will not be caught in a group, we need to matchPMorAMbut this group alone doesn’t matter.Next I match the file size or
<DIR>withCatching the result in a group.
The last part matches directory names or file names
Now that we captured all groups we can parse the values:
To check if the captured entry is a file or not you can just check if it is
<DIR>or a number.And the name is just what is left
Then you can use the groups to build the object, an example:
Further reading
String manipulation method
Another way to achieve this is to split the string which can be much faster:
Further reading