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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:06:49+00:00 2026-05-24T14:06:49+00:00

I have to parse a document containing groups of variable-value-pairs which is serialized to

  • 0

I have to parse a document containing groups of variable-value-pairs which is serialized to a string e.g. like this:

4^26^VAR1^6^VALUE1^VAR2^4^VAL2^^1^14^VAR1^6^VALUE1^^

Here are the different elements:

  1. Group IDs:

    4^26^VAR1^6^VALUE1^VAR2^4^VAL2^^1^14^VAR1^6^VALUE1^^

  2. Length of string representation of each group:

    4^26^VAR1^6^VALUE1^VAR2^4^VAL2^^1^14^VAR1^6^VALUE1^^

  3. One of the groups:

    4^26^VAR1^6^VALUE1^VAR2^4^VAL2^^1^14 ^VAR1^6^VALUE1^^

  4. Variables:

    4^26^VAR1^6^VALUE1^VAR2^4^VAL2^^1^14^VAR1^6^VALUE1^^

  5. Length of string representation of the values:

    4^26^VAR1^6^VALUE1^VAR2^4^VAL2^^1^14^VAR1^6^VALUE1^^

  6. The values themselves:

    4^26^VAR1^6^VALUE1^VAR2^4^VAL2^^1^14^VAR1^6^VALUE1^^

Variables consist only of alphanumeric characters.
No assumption is made about the values, i.e. they may contain any character, including ^.

Is there a name for this kind of grammar? Is there a parsing library that can handle this mess?

So far I am using my own parser, but due to the fact that I need to detect and handle corrupt serializations the code looks rather messy, thus my question for a parser library that could lift the burden.

  • 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-24T14:06:49+00:00Added an answer on May 24, 2026 at 2:06 pm

    The simplest way to approach it is to note that there are two nested levels that work the same way. The pattern is extremely simple:

    id^length^content^
    

    At the outer level, this produces a set of groups. Within each group, the content follows exactly the same pattern, only here the id is the variable name, and the content is the variable value.

    So you only need to write that logic once and you can use it to parse both levels. Just write a function that breaks a string up into a list of id/content pairs. Call it once to get the groups, and then loop through them calling it again for each content to get the variables in that group.

    Breaking it down into these steps, first we need a way to get “tokens” from the string. This function returns an object with three methods, to find out if we’re at “end of file”, and to grab the next delimited or counted substring:

    var tokens = function(str) {
        var pos = 0;
        return {
            eof: function() {
                return pos == str.length;
            },
            delimited: function(d) {
                var end = str.indexOf(d, pos);
                if (end == -1) {
                    throw new Error('Expected delimiter');
                }
                var result = str.substr(pos, end - pos);
                pos = end + d.length;
                return result;
            },
            counted: function(c) {
                var result = str.substr(pos, c);
                pos += c;
                return result;
            }
        };
    };
    

    Now we can conveniently write the reusable parse function:

    var parse = function(str) {
        var parts = {};
        var t = tokens(str);
        while (!t.eof()) {
            var id = t.delimited('^');
            var len = t.delimited('^');
            var content = t.counted(parseInt(len, 10));
            var end = t.counted(1);
            if (end !== '^') {
                throw new Error('Expected ^ after counted string, instead found: ' + end);
            }
            parts[id] = content;
        }
        return parts;
    };
    

    It builds an object where the keys are the IDs (or variable names). I’m asuming as they have names that the order isn’t significant.

    Then we can use that at both levels to create the function to do the whole job:

    var parseGroups = function(str) {
       var groups = parse(str);
       Object.keys(groups).forEach(function(id) {
         groups[id] = parse(groups[id]);
       });
       return groups;
    }
    

    For your example, it produces this object:

    {
      '1': { 
        VAR1: 'VALUE1' 
      },
      '4': {
        VAR1: 'VALUE1',
        VAR2: 'VAL2'
      } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string containing field/value pairs in xml format and want to parse
I have to parse an XML document that looks like this: <?xml version=1.0 encoding=UTF-8
I have this string containing a date I need to parse to get a
I have a class named xmlReader that have parse(String path) and parseXml(Document doc) methods.
I have strings containing fully qualified type names a la this MSDN document .
I have a collection of HTML documents for which I need to parse the
Let say I have to parse a hierarchical set of tags <tag> <subtag1 attr1=value1
Is there a easy way to do this? Or do I have to parse
I have to parse an HTML document into different new files. The problem is
Background: We have an XML document containing thousands of pseudocode functions. I've written 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.