1

My input string:

var s = "{1, >, [4,6,7,8], a, b, [x,y], d, 9}";

I'd like to remove the {} and get an array with each element separated by a comma EXCEPT when the comma is inside [] -- basically anything in brackets will be returned as its own element without the brackets.

Desired output List< String > or String[] whose contents are:

1
>
4,6,7,8
a
b
x,y
d
9

ETA: Here is my UnitText (xunit), which tests each of the patterns suggested by @washington-guedes, with a parameter to trim the input string of whitespaces. The test fails on both cases when the WS is cleaned.

    [Theory]
    [InlineData(@"([^{\s]+(?=(?:,|})))", false)]
    [InlineData(@"([^{\s]+(?=(?:,|})))", true)]
    [InlineData(@"([^{\s[\]]+(?=(?:]|,|})))", false)]
    [InlineData(@"([^{\s[\]]+(?=(?:]|,|})))", true)]
    public void SO(string pattern, bool trimWS)
    {
        //Arrange
        var exp = "{1, >, [4,6,7,8], a, b, [x,y], d, 9}";
        if (trimWS)
            exp = exp.Replace(" ", "");
        Match match = Regex.Match(exp, pattern);
        var list = new List<String>();
        while (match.Success)
        {
            list.Add(match.Value);
            match = match.NextMatch();
        }
        Assert.Equal(8, list.Count);
    }
2
  • Can sets of [...] be nested? And are there always spaces between the different major elements? Because right now, it seems like you can just remove the starting '{' and ending '}', split by ", ", and then remove any '['s and ']'s. Commented Jul 24, 2015 at 21:11
  • 1
    What have you tried? I'm glad to help, but StackExchange isn't a coding service, you're not supposed to say "here's my problem, go solve it." Instead, show us your proposed solution and we can help you refine it. Commented Jul 24, 2015 at 21:13

1 Answer 1

1

Try this Regex:

((?<=,\[)[^]]+)|((?<={)[^,}]+)|((?<=,)(?!\[)[^,}]+)

Regex live here.

Explaining:

(                  # start of capturing group
  (?<=,\[)         # starting with ",["
  [^]]+            # matches all till next "]"
)                  # end of capturing group

  |                # OR

(
    (?<={)         # starting with "{"
    [^,}]+         # matches all till next "," or "}"
)

  |                # OR

(
    (?<=,)(?!\[)   # starting with "," and not a "["
    [^,}]+         # matches all till next "," or "}"
)

Hope it helps.

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

5 Comments

Regex101 says that this specifically has a non-capturing group, which doesn't seem very useful... shouldn't there be capturing groups in there somewhere?
([^{\s]+(?=(?:,|}))) might work best - it's got the capture groups to make an array.
This matches fairly well, however is there a way to ignore whitespace?
If you s = s.Replace(" ", ""); before calling the regex, it returns a different set of matches
That works great. I was also looking for the version that included {}. I wish I could "read" your expression to understand it. I pasted that expression in regex101. Is the first "part" of the expression that you just added ((?<=,\[)[^]]+)

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.