-2

I need replace variable in string from user. For example:

User input: Ssad asdsdqwdq jdiqwj diqw jd qwld j {price-40%} asd asd asd

I know how replace only {price} but I don't know how to replace other cases.

I need support these cases:

{price}
{price-xx%}
{price+xx%}
{price-xx}
{price+xx}
{price/xx}
{price*xx}

And user can use variable {price} many times.

After user submit text, my app replace variable {price} or calc {price-xx%} and create a new string.

2
  • 1
    Show us the code you have tried with. Regular expression is what you need for this. Commented Feb 17, 2020 at 4:35
  • You need to use a regex to get the string between { } and then check this to parse that Commented Feb 17, 2020 at 5:01

2 Answers 2

0

If I understood your problem correctly then I think you can evaluate the whole expression without Replacing variables (might you have to change placements of variables)

First, add 'System.Data' name space in your project

then:

double price = 110;
double xx = 15;
double result = 0;

result = Convert.ToDouble(new DataTable().Compute($"({price-(price*xx)/100})", null));
Console.WriteLine("{price - xx%} = " + result);

result = Convert.ToDouble(new DataTable().Compute($"({price + (price * xx) / 100})", null));
Console.WriteLine("{price + xx%} = " + result);

result = Convert.ToDouble(new DataTable().Compute($"({price}-{xx})", null));
Console.WriteLine("{price - xx} = " + result);

result = Convert.ToDouble(new DataTable().Compute($"({price}+{xx})", null));
Console.WriteLine("{price + xx} = " + result);

result = Convert.ToDouble(new DataTable().Compute($"({price}/{xx})", null));
Console.WriteLine("{price / xx} = " + result);

result = Convert.ToDouble(new DataTable().Compute($"({price}*{xx})", null));
Console.WriteLine("{price * xx} = " + result);
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, but it's not what I need. I can explain again. User type to app e.g. "I will buy your computer for {price-10%} USD. Let me know." And the result will be "I will buy your computer for 90 USD". The price its 100 USD in this example. 100 - 10% = 90 USD. It is understandable? Thank you very much
0

https://github.com/davideicardi/DynamicExpresso/

static void Main(string[] args)
{
    int price = 100;

    Regex regex = new Regex(@"(?<=\{).*?(?=\})");

    string userInput = "Hi. I want to buy your computer. I can't offer {price} USD, but I can offer {price-(price/100)*10} USD";

    string text = userInput;

    foreach (var item in regex.Matches(text))
    {
        string expression = item.ToString().Replace("price", price.ToString());
        var interpreter = new Interpreter();
        var result = interpreter.Eval(expression);
        text = regex.Replace(text, result.ToString(),1);
        text = ReplaceFirst(text, "{", string.Empty);
        text = ReplaceFirst(text, "}", string.Empty);
    }

    Console.WriteLine("Result: " + text);
}

public static string ReplaceFirst(string text, string search, string replace)
{
    int pos = text.IndexOf(search);

    if (pos < 0)
    {
        return text;
    }

    return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
}

2 Comments

But the regex you are using is not correct. It'll match anything inside curly braces.
I know. User can't use curly braces in other cases, so it's ok.

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.