-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.

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
Adam Gajdečka
  • 305
  • 1
  • 4
  • 13
  • 1
    Show us the code you have tried with. Regular expression is what you need for this. – Sнаđошƒаӽ Feb 17 '20 at 04:35
  • You need to use a regex to get the string between { } and then check [this](https://stackoverflow.com/questions/21750824/how-to-convert-a-string-to-a-mathematical-expression-programmatically) to parse that – jalsh Feb 17 '20 at 05:01

2 Answers2

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);
S.ATTA.M
  • 409
  • 5
  • 10
  • 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 – Adam Gajdečka Feb 17 '20 at 12:34
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);
}
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
Adam Gajdečka
  • 305
  • 1
  • 4
  • 13