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 5941947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:10:55+00:00 2026-05-22T16:10:55+00:00

All the data I have to parse is currently stored into a StringBuilder and

  • 0

All the data I have to parse is currently stored into a StringBuilder and I would like to parse it into my class list:

StringBuilder data = new StringBuilder(length);

So I have my class assigned to a list:

public class Messages
{
    public DateTime Sent { get; set; }
    public string User {get; set; }
    public MessageType TypeUsed { get; set; }
    public string Message { get; set; }
}

public enum MessageType
{
    System,
    Info,
    Warn
}

public List<Messages> myList = new List<Messages>();

Now here are some messages samples that I need to parse:

[13:49:13] [System Message] <Username>  has openned blocked website 
[13:49:14] <Username> accessed file X
[13:52:46] [System Message] <Username>  has entered this room 
[13:52:49] [System Message] <Username>  has left this room 

My doubt here is what would be the best way to parse it.

Time is present in all messages.
Usernaem is always with <>
When there is no [System Message] or [Warn Message] it is a Info type message.
Message is the rest example:

has left this room
accessed file X
has openned blocked website

Now here is where I am still thinking what to use.

I could use a regex to extract each string something like this:

Regex getData = new Regex(@"^\[(\d{1,2}:\d{1,2}:\d{1,2})\] \[([A-Za-z]+)\] ");

But then I would basicly need to make several checks for each message so I was not so confortable with it.

Tought about using split for example:

string line = item.Replace("[", "").Replace("]", "");
string[] fields = line.Split(' ');

and then I would check the split cases would be easy to detect the MessageType but not so reliable I think.

I would like some advices and ideas of how I could go along with this ?

Maybe I am just overcomplicating the logic :/

  • 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-22T16:10:56+00:00Added an answer on May 22, 2026 at 4:10 pm

    A regex is probably most convenient here. Try this one:

    ^\[(\d{2}:\d{2}:\d{2})\]\s*(\[(System|Warn)[\w\s]*\])?\s*<([^>]*)>\s*(.*)$
    

    Translation:

    • Starting at the beginning of the line, match [##:##:##] into capture group 1
    • Then optionally match the System/Warn specifiers into capture group 2 and 3 (2 holds all the text in brackets, 3 only the System/Warn keyword)
    • Then capture the username inside angle brackets into capture group 4
    • And finally the message text in group 5

    By testing the contents of group 2 or 3 for each line you know what type of message it is. All the other fields are ready to use straight from the capture groups.

    Update:

    Here’s sample code as per the above:

    var regex = new Regex(@"^\[(\d{2}:\d{2}:\d{2})\]\s*(\[(System|Warn)[\w\s]*\])?\s*<([^>]*)>\s*(.*)$");
    var input = new[]
        {
            "[13:49:13] [System Message] <Username>  has openned blocked website", 
            "[13:49:14] <Username> accessed file X",
            "[13:52:46] [System Message] <Username>  has entered this room",
            "[13:52:49] [System Message] <Username>  has left this room"
        };
    
    foreach (var line in input) {
        var match = regex.Match(line);
        if (!match.Success) {
            throw new ArgumentException();
        }
    
        Console.WriteLine("NEW MESSAGE:");
        Console.WriteLine("     Time: " + match.Groups[1]);
        Console.WriteLine("     Type: " + match.Groups[2]);
        Console.WriteLine("     User: " + match.Groups[4]);
        Console.WriteLine("     Text: " + match.Groups[5]);
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a script parsing data from a Web Service, all examples I've
I'm trying to find all data items that have long/lats that are contained by
I have a C# Application I am creating that stores all data in SQL
HI all, I created a .net object (ex: A ) which contain data have
I have data from MySQL showing all organisations a customer got, with all details
I have some data structures: all_unordered_m is a big vector containing all the strings
One day I suspect I'll have to learn hadoop and transfer all this data
I have made Iphone applicatio. In my application whatever data i have recorded; all
I have a Visual Studio 2005/ C# ClickOnce application that gets all its data from a
I have the DGV bound to data and all the other controls properly. What

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.