需要使用括号拆分字符串

huangapple go评论66阅读模式
英文:

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

https://dotnetfiddle.net/wryFO5

答案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 = "";
     }
}

result:

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

huangapple
  • 本文由 发表于 2023年2月26日 21:20:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定