Given a string and a number:

int rowNumber = 5; var str = "%00002047{(11*rowNumber):D3}"; 

It is necessary to calculate the mathematical action (multiplication) between the constant ( 11 ) and the variable ( rowNumber ), after which the result is converted according to the format ( D3 ) and inserted into the initial row.

Mathematical actions can be +-/* .

The order of specifying a constant and a number can be either: (11*rowNumber) or (rowNumber*11) .

Sequencing:

 11*5=55 формат D3 = 055 вставка результата = "%00002047055"; 

How to select an action (multiplication, for example), a constant, and how to insert the result back into the string? Is there a typical solution, for example, through a regular expression?

  • And what is the question and with what problem did you have while solving the problem? - Regent
  • How to select an action (multiplication for example), a constant, and how to insert the result back into the string. If this is a typical solution for example through a regular expression, please give an example. - Aldmi
  • 1. rowNumber variable always rowNumber used? 2. In the mysterious format D3 you are able to translate the number? 3. Lines - a thing, in general, unchangeable, so that in the end a new line will be assembled. - Regent
  • yes rowNumber is always used, the format is not mysterious, but taken from from ToString ("D3"). It would be nice to add support for several mate. Actions for example rowNumber * 11 + 10. All that you wrote, I know. if not difficult write the parsing algorithm. - Aldmi
  • Well, no - one operation is solved in a "clumsy" way with the help of reg. expressions. But several operations / actions already require a full-fledged parser, so this is a completely different conversation. - Regent

1 answer 1

For these specific conditions, this is the case (provided that the string is valid).

Parsing string:

  1. Need to save characters to { .
  2. It is necessary to save the constant that comes either before the operator or after it.
  3. It is necessary to save the operator (which, accordingly, goes either up to a constant or after it).
  4. You need to save the format between : and } .
  5. Need to save characters after } .

Calculation of arithmetic expression:

  1. You need to decide what comes first: rowNumber or constant.
  2. Calculate the result based on the value of the operator and the order of the operands.

Formation of the final line:

  1. Conversion of arithmetic result by format from the 4th point of parsing.
  2. Concatenation of the 1st parsing point with converted arithmetic. result and with the 5th parsing point.

Summary Code:

 public static string Format(string str, int rowNumber) { string signPattern = @"([\+\-\/\*])"; Regex regex = new Regex(@"(.*)\{\((\w+" + signPattern + @"(\d+)|(\d+)" + signPattern + @"\w+)\)\:(\w+)\}(.*)"); Match match = regex.Match(str); if (!match.Success) { return str; } GroupCollection groups = match.Groups; bool directOrder = (groups[3].Length > 0); int operatorIndex = directOrder ? 3 : 6; int numberIndex = directOrder ? 4 : 5; int number = int.Parse(groups[numberIndex].Value); string nOperator = groups[operatorIndex].Value; int firstNumber = directOrder ? rowNumber : number; int secondNumber = directOrder ? number : rowNumber; int resultNumber = Calculate(firstNumber, secondNumber, nOperator); string format = groups[7].Value; string packedNumber = resultNumber.ToString(format); return groups[1].Value + packedNumber + groups[8].Value; } private static int Calculate(int a, int b, string nOperator) { switch (nOperator) { case "+": return a + b; case "-": return a - b; case "*": return a * b; case "/": return a / b; default: return 0; } } 

Regular expression:

  1. Some characters
  2. Characters {(
  3. (group of letters / numbers + operand + group of numbers) OR (group of numbers + operand + group of letters / numbers)
  4. Characters ):
  5. Letter / number group
  6. Character }
  7. Some characters

The "letter / number group" of clause 3 is the rowNumber . The “number group” of clause 3 is a constant. The "letter / number group" of clause 5 is a format.

Capture groups:

0: All expression found
1: Characters to {
2: Substring from operator and operands
3: Operator if rowNumber first operand
4: Constant if rowNumber first operand
5: Constant if rowNumber second operand
6: Operator if rowNumber second operand
7: Format
8: Characters after }

  • Thanks It works! And you can paint in more detail reg. expression and what is obtained in the collection of groups? - Aldmi
  • @Aldmi wrote at the end of the answer. - Regent
  • Thank you very much !! - Aldmi
  • @Aldmi to health. If that - the numbering of the groups in the answer is first incorrectly displayed. There are from 0 to 8 . - Regent