英文:
Need to split string with brackets
问题
I have a string which in parentheses and need to split it.
(Key value) (Key_Name val1 val2) (Key value) (Key_1 Value) (TESTS Key1 Key2 (Key3 Value)) (Key Value 0.0) (Key_1 (Key value) (Key value))
And i need the value it like
(Key value)
(Key_Name val1 val2)
(Key value)
(Key_1 Value)
(TESTS Key1 Key2 (Key3 Value))
(Key Value 0.0)
(Key_1 (Key value) (Key value))
Plz suggest.
i try this with below regex
((.*?))
英文:
I have a string which in parentheses and need to split it.
(Key value) (Key_Name val1 val2) (Key value) (Key_1 Value) (TESTS Key1 Key2 (Key3 Value)) (Key Value 0.0) (Key_1 (Key value) (Key value))
And i need the value it like
(Key value)
(Key_Name val1 val2)
(Key value)
(Key_1 Value)
(TESTS Key1 Key2 (Key3 Value))
(Key Value 0.0)
(Key_1 (Key value) (Key value))
Plz suggest.
i try this with below regex
\((.*?)\)
答案1
得分: 1
以下是您要的翻译内容:
你可以使用这个正则表达式:
string s = "(Key value) (Key_Name val1 val2) (Key value) (Key_1 Value) (TESTS Key1 Key2 (Key3 Value)) (Key Value 0.0) (Key_1 (Key value) (Key value))";
Regex regex = new Regex(@"\(([^)]*)\)");
string[] result = regex.Matches(s).Select(m => m.Groups[1].Value).ToArray();
https://dotnetfiddle.net/wryFO5
英文:
You can use this regex:
string s = "(Key value) (Key_Name val1 val2) (Key value) (Key_1 Value) (TESTS Key1 Key2 (Key3 Value)) (Key Value 0.0) (Key_1 (Key value) (Key value))";
Regex regex = new Regex(@"\(([^)]*)\)");
string[] result = regex.Matches(s).Select(m => m.Groups[1].Value).ToArray();
答案2
得分: 1
以下是已翻译的内容:
你可以通过使用`for循环`和`计算外部括号`来获得所需的结果:
string s = "(Key value) (Key_Name val1 val2) (Key value) (Key_1 Value) (TESTS Key1 Key2 (Key3 Value)) (Key Value 0.0) (Key_1 (Key value) (Key value))";
List<string> list = new List<string>();
string keyValue = "(";
int itemIndex = 0;
int numberP = 1;
for(int i=1; i<s.Length;i++)
{
if (s[i] == '(')
numberP++;
else if (s[i] == ')')
numberP--;
keyValue = keyValue + s[i];
if(numberP==0)
{
if(keyValue.Trim()!="")
list.Add(keyValue.Trim());
keyValue = "";
}
}
**[result][1]:**
(Key value)
(Key_Name val1 val2)
(Key value)
(Key_1 Value)
(TESTS Key1 Key2 (Key3 Value))
(Key Value 0.0)
(Key_1 (Key value) (Key value))
[1]: https://dotnetfiddle.net/31VKNt
请注意,由于代码部分不需要翻译,我已将其保留为原文。
英文:
You can reach the desired result by using the for loop
and counting the outer parentheses
:
string s = "(Key value) (Key_Name val1 val2) (Key value) (Key_1 Value) (TESTS Key1 Key2 (Key3 Value)) (Key Value 0.0) (Key_1 (Key value) (Key value))";
List<string> list = new List<string>();
string keyValue = "(";
int itemIndex = 0;
int numberP = 1;
for(int i=1; i<s.Length;i++)
{
if (s[i] == '(')
numberP++;
else if (s[i] == ')')
numberP--;
keyValue = keyValue + s[i];
if(numberP==0)
{
if(keyValue.Trim()!="")
list.Add(keyValue.Trim());
keyValue = "";
}
}
(Key value)
(Key_Name val1 val2)
(Key value)
(Key_1 Value)
(TESTS Key1 Key2 (Key3 Value))
(Key Value 0.0)
(Key_1 (Key value) (Key value))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论