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

1

3 Answers 3

1

A Regex like the the following should work.

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

You just need to read multiple matches as explained here.

Sign up to request clarification or add additional context in comments.

1 Comment

unrecognized escape sequence when trying to create regex
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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.