用 C# 中的函数标记数学表达式 [英] Tokenizing math expression with functions in C#

查看:42
本文介绍了用 C# 中的函数标记数学表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这很容易找到,但我没有成功.

I figured this would be easy to find, but I haven't been successful.

我需要能够标记以下表达式

I need to be able to tokenize the following expression

(4 + 5) + myfunc('两个词', 3, 5)

(4 + 5) + myfunc('two words', 3, 5)

进入

(
4
+
5
+
myfunc
(
'two words'
,
3
,
5
)

这似乎是一个常见的需求,但是我一直无法找到任何关于此的好的文档.这是我可以使用正则表达式做的事情吗?有人知道现有的方法吗?

It seems like this is probably a common need, however I haven't been able to find any good documentation on this out there. Is this something I could do using regex? Anybody know of an existing way to do this?

我使用的是 C#,但如果您有其他语言的答案,请不要害羞.

I'm using C#, but if you have the answer in another language, don't be shy.

提前致谢.

推荐答案

如果您正在寻找一个强大而强大的解决方案,那么您肯定应该考虑使用词法分析器(如 Antlr).但是,如果您需要的只是像您提供的那样的简单表达式的标记化,则可以很容易地实现这一结果:

If you are looking into a robust and powerful solution, you should definitively look into a lexical analyzer (like Antlr). However if what you need is just tokenization of simple expressions like the one you provided, you can achieve this result pretty easily:

// TODO Refactor and optimize this function
        public IList<string> TokenizeExpression(string expr)
        {
            // TODO Add all your delimiters here
            var delimiters = new[] { '(', '+', ')', ',' };
            var buffer = string.Empty;
            var ret = new List<string>();
            expr = expr.Replace(" ", "");
            foreach (var c in expr)
            {
                if (delimiters.Contains(c))
                {
                    if (buffer.Length > 0) ret.Add(buffer);
                    ret.Add(c.ToString(CultureInfo.InvariantCulture));
                    buffer = string.Empty;
                }
                else
                {
                    buffer += c;
                }
            }
            return ret;
        }

示例:

TokenizeExpression("(4 + 5) + myfunc('两个词', 3, 5)") Count = 14

TokenizeExpression("(4 + 5) + myfunc('two words', 3, 5)") Count = 14

[0]: "("
[1]: "4"
[2]: "+"
[3]: "5"
[4]: ")"
[5]: "+"
[6]: "myfunc"
[7]: "("
[8]: "'twowords'"
[9]: ","
[10]: "3"
[11]: ","
[12]: "5"
[13]: ")"

这篇关于用 C# 中的函数标记数学表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆