在C#中使用Regex通过带有一个分隔符异常的分隔符将字符串解析为数组/列表

时间:2015-07-24 21:09:10

标签: c# regex

我的输入字符串:

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

我想删除 {} 并获取一个数组,当逗号位于 [] 内时,每个元素用逗号EXCEPT分隔 - 基本上是任何内容括号将作为自己的元素返回,不带括号。

期望输出列表<字符串> 字符串[] ,其内容为:

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

ETA:这是我的UnitText(xunit),它测试@ washington-guedes建议的每个模式,并使用一个参数来修剪空格的输入字符串。清理WS时,两种情况下的测试都失败了。

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

1 个答案:

答案 0 :(得分:1)

试试这个正则表达式:

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

Regex live here.

解释

(                  # 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 "}"
)

希望它有所帮助。