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

  • Home
  • SEARCH
  • 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 6796937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:32:52+00:00 2026-05-26T18:32:52+00:00

I have a string like (Name := Sam&&Age=:17&& Score:=C6) and a class called Person

  • 0

I have a string like (Name := Sam&&Age=:17&& Score:=C6) and a class called Person with around 30 properties – Name, Age, Score, etc. How do I write a regex to parse the string, provided there is a variable number of properties?

Also I was thinking of doing some string replacements to convert the string to a proper JSON and then parse it. Does it sound like a good idea for production code?

I’m using C# 2010.

  • 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-26T18:32:52+00:00Added an answer on May 26, 2026 at 6:32 pm

    This regex should match your input string.

    \(\s*((?<PropertyName>.*?)\s*((:=)|(=:))\s*(?<PropertyValue>.*?)\s*(&&)?\s*)*\)
    

    Here’s what it means:

    \(                          Open paren
    \s*                         Optional whitespace
    (
        (?<PropertyName>.*?)    Property name group
        \s*                     Optional whitespace
        ((:=)|(=:))             Literal ':=' or '=:'
        \s*                     Optional whitespace
        (?<PropertyValue>.*?)   Property value group
        \s*                     Optional whitespace
        (&&)?                   Optional '&&' (the last one won't have one)
        \s*                     Optional whitespace
    )*                          The proceeding can repeat 0-many times
    \)                          Close paren
    

    With this, you can do a match against your string in C#:

    var regex = new Regex(
        @"\(\s*((?<PropertyName>.*?)\s*((:=)|(=:))\s*(?<PropertyValue>.*?)\s*(&&)?\s*)*\)");
    var match = regex.Match(yourString);
    

    Then loop through each property/value pair, setting the properties on your object. Setting the object properties will require some reflection and different code based on the object property types:

    var properyNames = match.Groups["PropertyName"].Captures;
    var properyValues = match.Groups["PropertyValue"].Captures;
    var numPairs = propertyNames.Count;
    
    var objType = yourObj.GetType();
    for (var i = 0; i < numPairs; i++)
    {
        var propertyName = propertyNames[i].Value;
        var theValue = propertyValues[i].Value;
    
        var property = objType.GetProperty(propertyName);
        object convertedValue = theValue;
        if (property.PropertyType == typeof(int))
            convertedValue = int.Parse(theValue);
        if (property.PropertyType == typeof(DateTime))
            // ....
        // etc....
    
        property.SetValue(yourObj, convertedValue, null);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a string like this: blablablamorecontentblablabla?name=michel&score=5&age=28&iliedabouttheage=true looks like a regular query string yes,
Say I have a structure like: class SomeObject Public Name as String Public Created
Say I have a few string like Hi my name is <Name> Hi <name>,
Ok, I have a hardcoded string I declare like this name = uPar Catégorie
If I have a string in Groovy like so... 'user.company.name' ... and I want
I have string like \LESSING\root\cimv2:Win32_UserAccount.Domain=LESSING,Name=Admin How to convert it to LESSING\Admin using Framework?
Let's presume that I have string like '=&?/;#+%' to be a part of my
I may have string like, Hello, %(name)s, how are you today, here is amount
I have list like: ['name','country_id', 'price','rate','discount', 'qty'] and a string expression like exp =
So, If I have a string like hello what is my name How can

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.