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

The Archive Base Latest Questions

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

Need some ideas how to solve this problem. I have a template file what

  • 0

Need some ideas how to solve this problem.
I have a template file what describes the line in the text file. For example:

Template

[%f1%]|[%f2%]|[%f3%]"[%f4%]"[%f5%]"[%f6%]

Text file

1234|1234567|123"12345"12"123456

Now i need to read in the fields from the text file. In the template file fields are described with [%some name%]. Allso in the template file there is set what the field separators are, in this example here there are | and ". The lenght of the fields can change through different files but the separators will stay the same. What would be the best way to read in the template and by template read in the text file?

EDIT: Text file has multiple rows, like this:

1234|1234567|123"12345"12"123456"\r\n
1234|field|123"12345"12"asdasd"\r\n
123sd|1234567|123"asdsadf"12"123456"\r\n
45gg|somedata|123"12345"12"somefield"\r\n

EDIT2: Ok, lets make it even harder. Some fields can contain binary data and i know the starting and end position of the binary data field. I should be able to mark those fields in the template and then the parser will know that this field is binary. How to solve this problem?

  • 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:36:10+00:00Added an answer on May 23, 2026 at 9:36 am

    I would create a regex based on the template and then parse the text file using that:

    class Parser
    {
        private static readonly Regex TemplateRegex =
            new Regex(@"\[%(?<field>[^]]+)%\](?<delim>[^[]+)?");
    
        readonly List<string> m_fields = new List<string>();
        private readonly Regex m_textRegex;
    
        public Parser(string template)
        {
            var textRegexString = '^' + TemplateRegex.Replace(template, Evaluator) + '$';
            m_textRegex = new Regex(textRegexString);
        }
    
        string Evaluator(Match match)
        {
            // add field name to collection and create regex for the field
            var fieldName = match.Groups["field"].Value;
            m_fields.Add(fieldName);
            string result = "(.*?)";
    
            // add delimiter to the regex, if it exists
            // TODO: check, that only last field doesn't have delimiter
            var delimGroup = match.Groups["delim"];
            if (delimGroup.Success)
            {
                string delim = delimGroup.Value;
                result += Regex.Escape(delim);
            }
            return result;
        }
    
        public IDictionary<string, string> Parse(string text)
        {
            var match = m_textRegex.Match(text);
            var groups = match.Groups;
    
            var result = new Dictionary<string, string>(m_fields.Count);
    
            for (int i = 0; i < m_fields.Count; i++)
                result.Add(m_fields[i], groups[i + 1].Value);
    
            return result;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some ideas on how I can best solve this problem. I have
I'm hoping people have some ideas to help solve this problem. I am developing
I have three buttons and need to save some data. I have a idea,
I need some help from the shell-script gurus out there. I have a .txt
I need some basic CMS functionality with rich text editing. On stack overflow there
lately i've been slamming my head against my desk to solve this Problem. Didn't
I have a problem that i am not sure how to solve efficiently. I
I need some advice as to how I easily can separate test runs for
I need some info on how to use margins and how exactly padding works.
I need some software to explore and modify some SQLite databases. Does anything similar

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.