0

Input: String input = "{True,True,False},{False,True,True},{False,True,True}"

Expected output: String[] output = new String[]("True,True,False", "False,True,True", "False,True,True");

I am trying to use Regex.Split, but unsuccessful with the pattern. Any hints?

6
  • 2
    what is data type of input? is it a string or Boolean nested array? Commented Oct 21, 2019 at 5:55
  • "expected output: array with elements" what does this mean exactly Commented Oct 21, 2019 at 5:56
  • 1
    It would be more helpful if you actually initialized variables in code to describe the input and output. Commented Oct 21, 2019 at 5:56
  • 1
    Please add your pattern. Maybe it's an easy fix. Commented Oct 21, 2019 at 6:00
  • 2
    regex101.com/r/aL0gbi/1 (match instead of split) Commented Oct 21, 2019 at 6:05

4 Answers 4

4

Here is my version:

using System;
using System.Linq;
using System.Text.RegularExpressions;

class Program
{
    public static void Main(string[] args)
    {
        String input = "{True,True,False},{False,True,True},{False,True,True}";

        var pattern = "{(.*?)}";
        var matches = Regex.Matches(input, pattern);

        var output2 = matches
                 .Select(m => m.Groups[1].ToString())
                 .ToList();

        foreach (var o in output2) Console.WriteLine(o);
    }
}

Output

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

4 Comments

I get the error CS1061: 'MatchCollection' does not contain a definition for 'Select' and no accessible extension method 'Select' accepting a first argument of type 'MatchCollection' could be found (are you missing a using directive or an assembly reference?)
You need using System.Linq. (Updated the answer)
I already added that(and reference to System.Xml.Linq), but still getting the error.
I've added the whole program source code to help you with the error.
3

You could try:

var result = input
  // Remove sorrounding { and }     
  .Trim(new char[] { '{', '}' })
  // Split by "},{"
  .Split(new string[] { "},{" }, StringSplitOptions.RemoveEmptyEntries);

Comments

0

You can trim first and last { } using .TrimStart() and .TrimEnd() respectively and then apply RegEx with below pattern

    string str = "{True,True,False},{False,True,True},{False,True,True}".TrimStart('{').TrimEnd('}');
    var result = Regex.Split(str, @"},{"); // result is an array of string.
    Console.WriteLine(string.Join(" ", result));

Output

True,True,False False,True,True False,True,True

.Net Fiddle

Comments

0

You can use:

string input = "{True,True,False},{False,True,True},{False,True,True}";

var pattern = @"{([A-Za-z,].+?)}";
var yourArray = Regex.Matches(input, pattern);

var Result= yourArray 
         .Select(m => m.Groups[1].ToString())
         .ToList();

foreach(var r in Result) {
   Console.WriteLine(r);
}

Your output :

True,True,False
False,True,True
False,True,True

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.