Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6062281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:01:59+00:00 2026-05-23T09:01:59+00:00

I have these strings as a response from a FTP server: 02-17-11 01:39PM <DIR>

  • 0

I have these strings as a response from a FTP server:

  1. 02-17-11 01:39PM <DIR> dec

  2. 04-06-11 11:17AM <DIR> Feb 2011

  3. 05-10-11 07:09PM 87588 output.xlsx

  4. 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:02:00+00:00Added an answer on May 23, 2026 at 9:02 am

    Regex method

    One approach is using this regex

    @"(\d{2}-\d{2}-\d{2} \d{2}:\d{2}(?:PM|AM)) (<DIR>|\d+) (.+)";
    

    I am capturing groups, so

    // Group 1 - Matches the DateTime
    (\d{2}-\d{2}-\d{2} \d{2}:\d{2}(?:PM|AM))
    

    Notice the syntax (?:xx), it means that the content here will not be caught in a group, we need to match PM or AM but this group alone doesn’t matter.

    Next I match the file size or <DIR> with

    // Group 2 - Matches the file size or <DIR>
    (<DIR>|\d+)
    

    Catching the result in a group.

    The last part matches directory names or file names

    // Group 3 - Matches the dir/file name
    (.+)
    

    Now that we captured all groups we can parse the values:

    DateTime.Parse(g[1].Value); // be careful with current culture
                                // a different culture may not work
    

    To check if the captured entry is a file or not you can just check if it is <DIR> or a number.

    IsFile = g[2].Value != "<DIR>"; // it is a file if it is not <DIR>
    

    And the name is just what is left

    Name = g[3].Value; // returns a string
    

    Then you can use the groups to build the object, an example:

    public class Files
    {
        public DateTime ModifiedTime { get; set; }
        public bool IsFile { get; set; }
        public string Name { get; set; }
    
        public Files(GroupCollection g)
        {
            ModifiedTime = DateTime.Parse(g[1].Value);
            IsFile = g[2].Value != "<DIR>";
            Name = g[3].Value;
        }
    }
    
    static void Main(string[] args)
    {
        var p = @"(\d{2}-\d{2}-\d{2} \d{2}:\d{2}(?:PM|AM)) (<DIR>|\d+) (.+)";
        var regex = new Regex(p, RegexOptions.IgnoreCase);
    
        var m1 = regex.Match("02-17-11 01:39PM <DIR> dec");
        var m2 = regex.Match("05-10-11 07:09PM 87588 output.xlsx");
    
        // DateTime: 02-17-11 01:39PM
        // IsFile  : false
        // Name    : dec
        var file1 = new Files(m1.Groups);
    
        // DateTime: 05-10-11 07:09PM
        // IsFile  : true
        // Name    : output.xlsx
        var file2 = new Files(m2.Groups);
    }
    

    Further reading

    • Regex class
    • Regex groups

    String manipulation method

    Another way to achieve this is to split the string which can be much faster:

    public class Files
    {
        public DateTime ModifiedTime { get; set; }
        public bool IsFile { get; set; }
        public string Name { get; set; }
    
        public Files(string line)
        {
            // Gets the date part and parse to DateTime
            ModifiedTime = DateTime.Parse(line.Substring(0, 16));
    
            // Gets the file information part and split
            // in two parts
            var fileBlock = line.Substring(17).Split(new char[] { ' ' }, 2);
    
            // first part tells if it is a file
            IsFile = fileBlock[0] != "<DIR>";
    
            // second part tells the name
            Name = fileBlock[1];
        }
    }
    
    static void Main(string[] args)
    {
        // DateTime: 02-17-11 01:39PM
        // IsFile  : false
        // Name    : dec
        var file3 = new Files("02-17-11 01:39PM <DIR> dec");
    
        // DateTime: 05-10-11 07:09PM
        // IsFile  : true
        // Name    : out put.xlsx
        var file4 = new Files("05-10-11 07:09PM 87588 out put.xlsx");
    }
    

    Further reading

    • String split
    • String.Split Method (Char[], Int32)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have strings with dates, and want to parse these into NSDate objects. Is
I have an object which I create from the response from a webservice. If
I am getting response from server side in the form of json array.I need
I have a JSON multi-level response that I need to deserialize and from the
I have these two situations: String s = aa; s = s + aa;
Let's say I have these two arrays: string[] arr1 = new string[2]{Hello, Stack} string[]
I have these classes. class Author{ Person person } class Person{ String lastName String
I have a class that contains two methods like these: public String getFoo(Int32 a)
Does Python have a pool of all strings and are they (strings) singletons there?
Is there a way to have multiline strings in VB.NET like Python a =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.