0

I have to parse string in C# as:

Some text [ first_name | User ] some other text. [ Your birthday is on yy/mm/dd | ]

Example:

Dear [ first_name | User ], How are you?. [ Your birthday is on yy/mm/dd | ]

So, I check for brackets [] in whole text, remove space before and after characters: [, |, ] and fill the first_name from database and if first_name is not available then replace with alternative text which is User in this case.

And if there is no birthday present in database then replace Your birthday is on yy/mm/dd with null.

I know regex is for string matching, but how do I do string replacement like: Replace("first_name", Name); in regex?

Is there a third party librabry for this?

1
  • This is too complex for regex. It is doable, but not worth the time and trouble. You should consider using a template engine like RazorEngine. Commented Nov 8, 2019 at 17:02

2 Answers 2

1

Regex can do this :) Please try something like that :

class Program
{
    static void Main(string[] args)
    {
        var processedText = Regex.Replace("Some text [first_name | User] some other text. [Your birthday is on yy / mm / dd | ]. " +
            "Dear [ first_name | User ], How are you?. [ Your birthday is on yy/mm/dd | ]",
            @"\[\s*(.*?)\s*\|\s*(.*?)\s*\]", new MatchEvaluator(ReplaceAction));

        Console.WriteLine(processedText);
    }

    public static bool DummyTryGetValueFromDb(string key, out string value)
    {
        Random randomizer = new Random(DateTime.UtcNow.Millisecond);

        if (randomizer.Next(100) > 50) // Successfully found in db :)
        {
            if (key == "first_name")
            {
                value = "Toto";
                return true;
            }
            else
            {
                value = "Your birthday is on your birthday date from db !";
                return true;
            }
        }
        else
        {
            value = string.Empty;
            return false;
        }
    }

    public static string ReplaceAction(Match match)
    {
        if (match.Groups.Count > 1)
        {
            var dataKeyGroup = match.Groups[1];
            if (DummyTryGetValueFromDb(dataKeyGroup.Value, out var valueFromDb))
                return valueFromDb;
            else if (match.Groups.Count > 2)
                return match.Groups[2].Value;
            else
                return "[Not found from db, no alternative value]"; // you can throw exception
        }
        else
            return "[Bad syntax]"; // you can throw exception
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

You just hard-coded a solution for "[Your birthday is on yy / mm / dd | ]". Although OP's example isnt the best.
You're right : the main thing is that the structure answers the question, the details of data access and date conversion are still to be considered.
0
Regex.Replace("Dear [ first_name | User ], How are you?. [ Your birthday is on yy/mm/dd | ]", "Your Pattern", firstName ?? User)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.