Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
I have a string like
C#
string mainstring  =" Daily @f4(@f1(@f3(@f5(P1)))|@f1()) Report @f5()";


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.

C#
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. 
Posted
Comments
CHill60 23-Apr-15 6:40am    
Can you give a better example of what you are trying to do / What you expect to happen in each iteration of the loop
sachi Dash 23-Apr-15 7:09am    
It is nothing just function within a function ! I need to find out the function with its closing parenthesis. Each function return a string. At the End of the work mainstring will will replace with the value and it will looke like: Daily consumption Report 02-04-2015" instead of Daily @f4(@f1(@f3(@f5(P1)))|@f1()) Report @f5(). So I need to get the function with exact closing brackets. If I want to get @f5 I need to get @f5(p1)then it will return a value. If I want get @f1 I need to get @f1(@f3(@f5(P1)))that will return a value. After getting the return value of @f1 The string will replace @f1(@f3(@f5(P1))) by this return value.

1 solution

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.
 
Share this answer
 
Comments
Sascha Lefèvre 23-Apr-15 8:34am    
My 5
Maciej Los 23-Apr-15 8:56am    
5ed!
Sergey Alexandrovich Kryukov 29-Apr-15 10:41am    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900