且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用c#获取括号之间的单词

更新时间:2023-02-18 14:40:06

为此简单的文字:

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