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

我正在尝试为以下示例编写公式的正则表达式。

  1. C=A+B  => Output for match will be {A, +, B}

  2. D= C+50 => Output for match will be{C, +, 50}

  3. E = (A+B)*C -100 => Output for match will be{(, A, +, B, ), *, C, -, 100}

我试过正则表达式

[A-Z(\d*)*+/-]

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

但它没有给出正确的输出A+50


料青山看我应如是
浏览 143回答 2
2回答

蝴蝶不菲

我建议FSM(有限状态机)而不是正则表达式。3我们这里有状态:既不是变量,也不是数字0变量内&nbsp;1数以内2代码:private static IEnumerable<string> Parse(string formula) {&nbsp; int state = 0;&nbsp; StringBuilder buffer = new StringBuilder();&nbsp; foreach (var c in formula) {&nbsp; &nbsp; if (state == 0) { // neither var nor number&nbsp; &nbsp; &nbsp; if (char.IsWhiteSpace(c))&nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; if (char.IsDigit(c)) {&nbsp; &nbsp; &nbsp; &nbsp; buffer.Append(c);&nbsp; &nbsp; &nbsp; &nbsp; state = 2;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; else if (char.IsLetter(c)) {&nbsp; &nbsp; &nbsp; &nbsp; buffer.Append(c);&nbsp; &nbsp; &nbsp; &nbsp; state = 1;&nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; else&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; yield return c.ToString();&nbsp; &nbsp; }&nbsp; &nbsp; else if (state == 1) { // within variable&nbsp; &nbsp; &nbsp; if (char.IsDigit(c) || char.IsLetter(c))&nbsp; &nbsp; &nbsp; &nbsp; buffer.Append(c);&nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; yield return buffer.ToString();&nbsp; &nbsp; &nbsp; &nbsp; buffer.Clear();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; state = 0;&nbsp; &nbsp; &nbsp; &nbsp; if (!char.IsWhiteSpace(c))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return c.ToString();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; else if (state == 2) { // within number&nbsp; &nbsp; &nbsp; if (char.IsDigit(c))&nbsp; &nbsp; &nbsp; &nbsp; buffer.Append(c);&nbsp; &nbsp; &nbsp; else if (char.IsLetter(c)) {&nbsp; &nbsp; &nbsp; &nbsp; // 123abc we turn into 123 * abc&nbsp; &nbsp; &nbsp; &nbsp; yield return buffer.ToString();&nbsp; &nbsp; &nbsp; &nbsp; buffer.Clear();&nbsp; &nbsp; &nbsp; &nbsp; state = 1;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; yield return "*";&nbsp; &nbsp; &nbsp; &nbsp; buffer.Append(c);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; yield return buffer.ToString();&nbsp; &nbsp; &nbsp; &nbsp; buffer.Clear();&nbsp; &nbsp; &nbsp; &nbsp; state = 0;&nbsp; &nbsp; &nbsp; &nbsp; if (!char.IsWhiteSpace(c))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return c.ToString();&nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; }&nbsp; }&nbsp;&nbsp; if (buffer.Length > 0)&nbsp; &nbsp; yield return buffer.ToString();}演示:&nbsp; string[] tests = new string[] {&nbsp; &nbsp; "C=A+B",&nbsp; &nbsp; "D= C+50",&nbsp; &nbsp; "E = (A+B)*C -100",&nbsp; };&nbsp; string result = string.Join(Environment.NewLine, tests&nbsp; &nbsp; .Select(test => new {&nbsp; &nbsp; &nbsp; formula = test,&nbsp; &nbsp; &nbsp; parsed = Parse(test)&nbsp; &nbsp; &nbsp; &nbsp; .SkipWhile(term => term != "=") // we don't want "C = " or alike part&nbsp; &nbsp; &nbsp; &nbsp; .Skip(1)&nbsp; &nbsp; })&nbsp; &nbsp; .Select(test => $"{test.formula,-20} => {string.Join(", ", test.parsed)}"));&nbsp;Console.Write(result);结果:C=A+B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => A, +, BD= C+50&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => C, +, 50E = (A+B)*C -100&nbsp; &nbsp; &nbsp;=> (, A, +, B, ), *, C, -, 100

繁花不似锦

|对单个项目(例如图案)使用(或)\d+|\W|\w转换为任何数字或任何非字母字符或任何字母字符。
打开App,查看更多内容
随时随地看视频慕课网APP