从定义的分隔符之间的字符串中提取所有子串到字符串数组

时间:2022-09-13 12:24:48

I need some kind of method , using regex or split I don't know that does the following.

我需要某种方法,使用正则表达式或拆分我不知道如何做以下。

I have string that looks like this:

我有这样的字符串:

ls 0 
[0,86,180]
ls 1 
[1,2,200]
ls 2 
[2,3,180]
ls 3 
[3,4,234]

...and so on. I want everything between parenthesis [ ] to be one string inside string array, and everything else disregard

...等等。我希望括号[]之间的所有内容都是字符串数组中的一个字符串,而其他一切都无视

3 个解决方案

#1


1  

A Regex like the the following should work.

像下面的正则表达式应该工作。

(\[[\d]*,[\d]*,[\d]*\]*)

(\ [[\ d] *,[\ d] *,[\ d] * \] *)

You just need to read multiple matches as explained here.

您只需阅读多个匹配项,如此处所述。

#2


1  

This may give you the whole idea and steps:

这可能会给你整个想法和步骤:

var yourString = @"ls 0 
        [0,86,180]
        ls 1 
        [1,2,200]
        ls 2 
        [2,3,180]
        ls 3 
        [3,4,234]";

var result = yourString.Split(new char[] { '\n' })                //split
                       .Where(i => i.Contains('['))               //filter
                       .Select(i => i.Replace("[", string.Empty)  //prepare
                                     .Replace("]", string.Empty))
                       .ToList();

var newArray = string.Join(",", result);                          //merge the result

#3


0  

In case number blocks aren't always in sets of threes

如果数字块不总是三组

 var myString = "ls 0 
    [0,86,190]
    ls 1 
    [1,2,300]
    ls 2 
    [2,3,185]
    ls 3 
    [3,4,2345]";

    Regex pattern = new Regex(@"\[[\d,]*\]*");
    Match numbers = pattern.Match(myString);

    if(numbers.Success)
    {
      do {
          var val = numbers.Value;

          // code for each block of numbers

          numbers.NextMatch();
          } while(numbers.Success)
    }

#1


1  

A Regex like the the following should work.

像下面的正则表达式应该工作。

(\[[\d]*,[\d]*,[\d]*\]*)

(\ [[\ d] *,[\ d] *,[\ d] * \] *)

You just need to read multiple matches as explained here.

您只需阅读多个匹配项,如此处所述。

#2


1  

This may give you the whole idea and steps:

这可能会给你整个想法和步骤:

var yourString = @"ls 0 
        [0,86,180]
        ls 1 
        [1,2,200]
        ls 2 
        [2,3,180]
        ls 3 
        [3,4,234]";

var result = yourString.Split(new char[] { '\n' })                //split
                       .Where(i => i.Contains('['))               //filter
                       .Select(i => i.Replace("[", string.Empty)  //prepare
                                     .Replace("]", string.Empty))
                       .ToList();

var newArray = string.Join(",", result);                          //merge the result

#3


0  

In case number blocks aren't always in sets of threes

如果数字块不总是三组

 var myString = "ls 0 
    [0,86,190]
    ls 1 
    [1,2,300]
    ls 2 
    [2,3,185]
    ls 3 
    [3,4,2345]";

    Regex pattern = new Regex(@"\[[\d,]*\]*");
    Match numbers = pattern.Match(myString);

    if(numbers.Success)
    {
      do {
          var val = numbers.Value;

          // code for each block of numbers

          numbers.NextMatch();
          } while(numbers.Success)
    }