如何使用预期的括号拆分字符串? [英] How can I split string with expected parenthesis ?

查看:92
本文介绍了如何使用预期的括号拆分字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似
的字符串

I have a string like

string mainstring  =" Daily @f4(@f1(@f3(@f5(P1)))|@f1()) Report @f5()";





这里@ f1,@ f2,@ f3等等都是一个功能。我有一个列表FunctionList,其中包含所有这些函数名称。每个函数返回一个值,然后我将得到完整的句子。





Here @f1, @f2, @f3 etc. each of them is a function. I have a list "FunctionList" with all these name of function. Each function return a value and then I will get the complete sentence.

for(int i=0; i<FunctionList.Count;i++)
            {
                if (mainstring.Contains(FunctionList[i]))
                {
                   // Suppose list contains @f1 so it is also in the mainstring. So I want to get this portin "@f1(@f3(@f5(P1)))" I can locate index where @f1 is stared in the string but I don't know how can I get this mainstring with expected closing bracket !  
                }
            }


Please help me to find out the portion from the mainstring with expected closing parenthesis. 

推荐答案

基本上你是在尝试解析字符串中的语法。



请参阅此CP文章解析C#3.0第1部分的表达式语法支持 - PEG Lib和Parser Generator [ ^ ]也引用了这个发布 [ ^ ]包含一些其他想法。



这是一个学术论文 [ ^ ]关于这个主题。



因为你只是真正寻找括号和功能您希望能够简化主题的名称。
Essentially you are attempting to parse the grammar in the string.

See this CP article Parsing Expression Grammar Support for C# 3.0 Part 1 - PEG Lib and Parser Generator[^] which is also referenced from this post[^] that includes some alternative ideas.

Here's an academic paper[^] on the subject as well.

As you are only really looking for brackets and function names you will hopefully be able to simplify the topic.


这篇关于如何使用预期的括号拆分字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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