Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 包含数字、字母、表达式和括号的公式的模式_C#_Regex_Parsing - Fatal编程技术网

C# 包含数字、字母、表达式和括号的公式的模式

C# 包含数字、字母、表达式和括号的公式的模式,c#,regex,parsing,C#,Regex,Parsing,我试图为下面的示例编写一个正则表达式 C=A+B=>匹配的输出将是{A,+,B} D=C+50=>匹配的输出将是{C,+,50} E=A+B*C-100=>匹配的输出将是{,A,+,B,*,C,-,100} 我尝试使用正则表达式 这个的输出是{A,+,5,0} 但是它不能为+50给出正确的输出,我建议用FSM有限状态机代替正则表达式。我们这里有3个州: 既不是变量,也不是数字0 在变量1内 2号以内 代码: 结果: 将|或用于单个项目,例如图案 \d+|\W |\W 它可以转换为任何数字、任何非

我试图为下面的示例编写一个正则表达式

C=A+B=>匹配的输出将是{A,+,B} D=C+50=>匹配的输出将是{C,+,50} E=A+B*C-100=>匹配的输出将是{,A,+,B,*,C,-,100} 我尝试使用正则表达式

这个的输出是{A,+,5,0}

但是它不能为+50给出正确的输出,我建议用FSM有限状态机代替正则表达式。我们这里有3个州:

既不是变量,也不是数字0 在变量1内 2号以内 代码:

结果:

将|或用于单个项目,例如图案

\d+|\W |\W


它可以转换为任何数字、任何非字母字符或任何字母字符。

正则表达式是分析表达式的糟糕工具。还有一些其他的工具和技术要好得多。您可以从阅读问题标签上的信息开始。
[A-Z(\d*)*+/-]
private static IEnumerable<string> Parse(string formula) {
  int state = 0;

  StringBuilder buffer = new StringBuilder();

  foreach (var c in formula) {
    if (state == 0) { // neither var nor number
      if (char.IsWhiteSpace(c))
        continue;

      if (char.IsDigit(c)) {
        buffer.Append(c);
        state = 2;
      }
      else if (char.IsLetter(c)) {
        buffer.Append(c);
        state = 1;
      } 
      else 
        yield return c.ToString();
    }
    else if (state == 1) { // within variable
      if (char.IsDigit(c) || char.IsLetter(c))
        buffer.Append(c);
      else {
        yield return buffer.ToString();
        buffer.Clear(); 

        state = 0;

        if (!char.IsWhiteSpace(c))
          yield return c.ToString();
      }
    }
    else if (state == 2) { // within number
      if (char.IsDigit(c))
        buffer.Append(c);
      else if (char.IsLetter(c)) {
        // 123abc we turn into 123 * abc
        yield return buffer.ToString();
        buffer.Clear();

        state = 1; 

        yield return "*";

        buffer.Append(c);
      }
      else {
        yield return buffer.ToString();
        buffer.Clear();

        state = 0;

        if (!char.IsWhiteSpace(c))
          yield return c.ToString();
      } 
    }
  } 

  if (buffer.Length > 0)
    yield return buffer.ToString();
}
  string[] tests = new string[] {
    "C=A+B",
    "D= C+50",
    "E = (A+B)*C -100",
  };

  string result = string.Join(Environment.NewLine, tests
    .Select(test => new {
      formula = test,
      parsed = Parse(test)
        .SkipWhile(term => term != "=") // we don't want "C = " or alike part
        .Skip(1)
    })
    .Select(test => $"{test.formula,-20} => {string.Join(", ", test.parsed)}"));

 Console.Write(result);
C=A+B                => A, +, B
D= C+50              => C, +, 50
E = (A+B)*C -100     => (, A, +, B, ), *, C, -, 100