Click here to Skip to main content
15,896,502 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am working on a project where i have a requirement to get the words or text written between parenthesis and coloring it. for that i have used a code given below code using Indexof
C#
int start1 = paragraph1.IndexOf("(") + 1;
                           int end1 = paragraph1.IndexOf(")", start1);
                           if (start1 != -1 && end1 != -1)
                           {
                               string result = paragraph1.Substring(start1, end1 - start1);

                               string[] wrsstr = result.Split(' ');

                               for (int lst = 0; lst < wrsstr.Count(); lst++)
                               {
                                   if (strwords == wrsstr[lst])
                                   {
                                       if (Enumerable.Range(start1, end1).Contains(Convert.ToInt32(words1[i, 1])))
                                       {
                                           selection.SetRange(Convert.ToInt32(start1), Convert.ToInt32(end1));
                                           Globals.ThisAddIn.Application.Selection.Font.ColorIndex = Word.WdColorIndex.wdPink;
                                       }
                                   }

                               }
                           }

the above code gives text between parenthesis for first text (is) using Indexof .but my paragraph is having 3 such parenthesis .
eg: this (is) an (example) string given (for) text between (parenthesis).

the below code gives me the last word(parenthesis) using lastindex of. how to get the text between parenthesis in the middle.(example) and (for). is there any generic way to do this i want indexes along with the word to check for word and check for indexes and color it. word1[i,1] contains index of word.

C#
int start2 = paragraph1.LastIndexOf(")") - 1;
                            int end2 = paragraph1.LastIndexOf("(", start2);
                            if (start2 != -1 && end2 != -1)
                            {
                                string result2 = paragraph1.Substring(end2, start2 - end2);
                                string[] wrsstr2 = result2.Split(' ');

                                for (int lst2 = 0; lst2 < wrsstr2.Count(); lst2++)
                                {
                                    if (strwords == wrsstr2[lst2])
                                    {
                                        if (Enumerable.Range(end2, start2).Contains(Convert.ToInt32(words1[i, 1])))
                                        {
                                            selection.SetRange(Convert.ToInt32(end2+1), Convert.ToInt32(start2));
                                            Globals.ThisAddIn.Application.Selection.Font.ColorIndex = Word.WdColorIndex.wdPink;
                                        }
                                    }

                                }
                            }
Posted
Updated 17-Jun-15 5:21am
v2
Comments
PIEBALDconsult 17-Jun-15 12:13pm    
What about "(parentheses in quotes)"? And \(escaped parentheses\) ? And, as Griff mentioned, ((nested parentheses))?

For that simple a text:
this (is) an (example) string given (for) text between (parenthesis).
It's relatively simple - you just need to keep processing, starting each time from the index of the closing parenthesis you just used.
So just change your code to add a loop, and keep looping until IndexOf returns a negative number when looking for a '(', and update start1 each time your finish colouring a section.

But...you might want to think a little more carefully about what you are doing: nested parentheses will cause you real problems!
this (is) an (example string given (for) text) between (parenthesis).
Would need you to colour the whole of "example string given (for) text" not stop after the first ')'.
 
Share this answer
 
Comments
LLLLGGGG 17-Jun-15 17:46pm    
He can use an int variable and count the occurrencies of '(' then each time he encounters a ')' he subtracts one from that variable and once he reaches 0, he colours the range...
OriginalGriff 17-Jun-15 17:50pm    
Indeed he can.
But it's his homework, so it's better he comes up with ideas himself! :laugh:
 
Share this answer
 
Refer here C# Regular Expression Get Text Between Brackets

C#
string regularExpressionPattern = @"\((.*?)\)";

     string inputText = "Find string inside brackets (C#.net) and (Vb.ne); example in (ASP.net);

     Regex re = new Regex(regularExpressionPattern);

     foreach (Match m in re.Matches(inputText))
     {
       Console.WriteLine(m.Value);
     }
 
Share this answer
 
v2

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