如何使用c#获取括号之间的单词 [英] how to get the words between parenthesis using c#

查看:66
本文介绍了如何使用c#获取括号之间的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在开展一个项目,我要求在括号和着色之间写下文字或文字。因为我使用了以下代码给出的代码使用Indexof

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

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;
                                       }
                                   }

                               }
                           }



上面的代码在第一个文本的括号之间给出文字(是)使用Indexof。但我的段落有3个这样的括号。

例如:this(是)一个(示例)字符串给出(for)文本(括号)之间。



下面的代码给出了使用lastindex的最后一个单词(括号)。如何在中间的括号之间得到文本。(例子)和(for)。有没有任何通用的方法来做到这一点我希望索引与单词检查单词并检查索引和颜色。 word1 [i,1]包含单词索引。




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.

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;
                                        }
                                    }

                                }
                            }

推荐答案

为此简单的文字:

For that simple a text:
this (is) an (example) string given (for) text between (parenthesis).

它相对简单 - 你只需要继续处理,每次从你刚刚使用的右括号的索引开始。

所以只需改变你的代码来添加循环,并保持循环,直到IndexOf返回负数时对于'(',并在每次完成着色部分时更新start1。



但是......你可能想要更仔细地思考一下你是什么做:嵌套括号会给你带来真正的问题!

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).

需要你为整个示例s着色给予(for)文本不要在第一个')之后停止。

Would need you to colour the whole of "example string given (for) text" not stop after the first ')'.


Balanced Regex。 :很酷:



深度分析[ ^ ]
Balanced Regex. :cool:

In Depth with .NET RegEx Balanced Grouping[^]


请参阅此处 C#正则表达式在括号内获取文本



Refer here C# Regular Expression Get Text Between Brackets

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);
     }


这篇关于如何使用c#获取括号之间的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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