简体   繁体   English

使用正则表达式拆分带有变量的方程 C#

[英]using regex to split equations with variables C#

I've been struggling with this for quite awhile (not being a regex ninja), searching stackoverflow and through trial an error.我已经为此苦苦挣扎了一段时间(不是正则表达式忍者),搜索 stackoverflow 并通过试验出错。 I think I'm close, but there are still a few hiccups that I need help sorting out.我想我已经接近了,但仍有一些问题需要我帮助解决。

The requirements are such that a given equation, that includes variables, exponents, etc, are split by the regex pattern after variables, constants, values, etc. What I have so far要求是,包括变量、指数等在内的给定方程在变量、常量、值等之后由正则表达式模式分割。到目前为止我所拥有的

     Regex re = new Regex(@"(\,|\(|\)|(-?\d*\.?\d+e[+-]?\d+)|\+|\-|\*|\^)");
     var tokens = re.Split(equation)

So an equation such as所以一个方程如

    2.75423E-19* (var1-5)^(1.17)* (var2)^(1.86)* (var3)^(3.56)

should parse to应该解析为

     [2.75423E-19 ,*, (, var1,-,5, ), ^,(,1.17,),*....,3.56,)]

However the exponent portion is getting split as well which I think is due to the regex portion: |+|-.然而,指数部分也被拆分了,我认为这是由于正则表达式部分:|+|-。

Other renditions I've tried are:我尝试过的其他演绎是:

    Regex re1 = new Regex(@"([\,\+\-\*\(\)\^\/\ ])"); and 
    Regex re = new Regex(@"(-?\d*\.?\d+e[+-]?\d+)|([\,\+\-\*\(\)\^\/\ ])");

which both have there flaws.两者都有缺陷。 Any help would be appreciated.任何帮助,将不胜感激。

For the equations like the one posted in the original question, you can use对于原始问题中发布的方程,您可以使用

[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?|[-^+*/()]|\w+

See regex demo正则表达式演示

The regex matches:正则表达式匹配:

  • [0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)? - a float number - 浮点数
  • | - or... - 或者...
  • [-^+*/()] - any of the arithmetic and logical operators present in the equation posted [-^+*/()] - 发布的等式中存在的任何算术和逻辑运算符
  • | - or... - 或者...
  • \\w+ - 1 or more word characters (letters, digits or underscore). \\w+ - 1 个或多个单词字符(字母、数字或下划线)。

For more complex tokenization, consider using NCalc suggested by Lucas Trzesniewski 's comment .对于更复杂的符号化,可以考虑使用NCalc建议由卢卡斯Trzesniewski评论

C# sample code : C# 示例代码

var line = "2.75423E-19* (var1-5)^(1.17)* (var2)^(1.86)* (var3)^(3.56)";
var matches = Regex.Matches(line, @"[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?|[-^+*/()]|\w+");
foreach (Match m in matches)
    Console.WriteLine(m.Value);

And updated code for you to show that Regex.Split is not necessary here:并为您更新了代码以显示此处不需要Regex.Split

var result = Regex.Matches(line, @"\d+(?:[,.]\d+)*(?:e[-+]?\d+)?|[-^+*/()]|\w+", RegexOptions.IgnoreCase)
             .Cast<Match>()
             .Select(p => p.Value)
             .ToList();

Also, to match formatted numbers, you can use \\d+(?:[,.]\\d+)* rather than [0-9]*\\.?[0-9]+ or \\d+(,\\d+)* .此外,要匹配格式化的数字,您可以使用\\d+(?:[,.]\\d+)*而不是[0-9]*\\.?[0-9]+\\d+(,\\d+)*

So I think I've got a solution thanks to @stribizhev solution lead me to the regex solution所以我想我已经有了一个解决方案,这要归功于@stribishev 解决方案让我找到了正则表达式解决方案

            Regex re = new Regex(@"(\d+(,\d+)*(?:.\d+)?(?:[eE][-+]?[0-9]+)?|[-^+/()]|\w+)");
            tokenList = re.Split(InfixExpression).Select(t => t.Trim()).Where(t => t != "").ToList();  

When split gives me the desired array.当 split 给我所需的数组时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM