0

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user1480742
  • 145
  • 12
  • possible duplicate of [How do I extract text that lies between parentheses (round brackets)?](http://stackoverflow.com/questions/378415/how-do-i-extract-text-that-lies-between-parentheses-round-brackets) – Lex Webb Sep 25 '15 at 14:50

3 Answers3

1

A Regex like the the following should work.

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

You just need to read multiple matches as explained here.

Community
  • 1
  • 1
Joel Bourbonnais
  • 2,618
  • 3
  • 27
  • 44
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
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
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)
    }
Matt
  • 1,245
  • 2
  • 17
  • 32