2016-09-20 74 views
1

这里是有问题的字符串的示例:C#字符串处理非分隔字符串列出

[952,M] [782,M] [782] {2[373,M]} [1470] [352] [235] [234] {3[610]}{3[380]} [128] [127] 

我已经加入了空格,但它确实没有帮助击穿。我想要做的是将方括号中的每个“字段”添加到字符串列表中。我可以处理的下一个问题是一些字段也有一个逗号分隔的部分,我可以在事后分离。真正的问题在于花括号。例如{2[373,M]}方括号外的数字是方括号的重复。

对于我的生活,我无法找到一种方法,我可以一直将该行分割成一个字符串列表。

准代码如下:

for(i = 0 to string.length) 
{ 
    if string.substring(i,1) = "]" 
     int start1 = i 
    elseif string.substring(i,1)="[" 
     int start1 = i 
    elseif string.substring(i,1) = "{" 
     int start2 = i 
    elseif string.substring(i,1) = "}" 
     int end2 = i 
} 

我想过使用代码的想法上面串出每一个“场”,但在大括号中还含有方括号。任何想法将不胜感激。

+1

是在'{3 [610]} {3 [380]}'一个错字或东西,你可以在预期缺乏空间的你输入? –

+0

1)你想要什么* {{2 [373,M]}'变成?两串'373,M'? 2)'string.substring(i,1)==“]”'是荒谬的。只要'string [i] ==']''。 – itsme86

+0

Asad Saeeduddin这是我的字符串操作错误。 – jhdeval

回答

1
var s = "[952,M] [782,M] [782] {2[373,M]} [1470] [352] [235] [234] {3[610]}{3[380]} [128] [127]"; 

var s2 = Regex.Replace(s, @"\{(\d+)(\[[^]]+\])\}", m => string.Concat( 
    Enumerable.Repeat(m.Groups[2].Value, int.Parse(m.Groups[1].Value)))); 

var a = s2.Split("[] ".ToArray(), StringSplitOptions.RemoveEmptyEntries); 

// s2 = "[952,M] [782,M] [782] [373,M][373,M] [1470] [352] [235] [234] [610][610][610][380][380][380] [128] [127]" 
// a = {"952,M","782,M","782","373,M","373,M","1470","352","235","234","610","610","610","380","380","380","128","127"} 
0

你可以使用正则表达式。

编辑:这个管理问题,逗号和repetititon:

 var regex3 = new Regex(@"(\B\[([a-zA-Z0-9\,]+)\])|(\{(\d+)\[([a-zA-Z0-9\,]+)\]\})"); 
     var stringOne = "[952,M] [782,M] [782] {2[373,M]} [1470] [352] [235] [234] {3[610]}{3[380]} [128] [127]"; 
     var matches = regex.Matches(stringOne); 

     var listStrings = new List<string>(); 

     foreach (Match match in matches) 
     { 
      var repetitor = 1; 
      string value = null; 
      if (match.Groups[1].Value == string.Empty) 
      { 
       repetitor = int.Parse(match.Groups[4].Value); 
       value = match.Groups[5].Value; 
      } 

      else 
      { 
       value = match.Groups[2].Value; 
      } 

      var values = value.Split(','); 
      for (var i = 0; i < repetitor; i++) 
      { 
       listStrings.AddRange(values); 
      } 
     } 
+0

这是死的关闭它唯一的问题是它是大括号。方括号外的数字表示重复。这个{3 [610]}将被添加到列表中3次。 – jhdeval

1

如果我理解正确的话,你想拆用方括号括字符,当他们有大括号重复指定号码里面的内容的时代。

您可以提取您需要使用正则表达式的所有信息,包括你需要重复支架

var input = @"[952,M] [782,M] [782] {2[373,M]} 
       [1470] [352] [235] [234] {3[610]}{3[380]} [128] [127]"; 

var pattern = @"((:?\{(\d+)(.*?)\})|(:?\[.*?\]))"; 

MatchCollection matches = Regex.Matches(input, pattern); 

var ls = new List<string>(); 

foreach(Match match in matches) 
{ 
    // check if the item has curly brackets 
    // The captures groups will be different if there were curly brackets 

    // If there are brackets than the 4th capture group 
    // will have the value of the square brackets and it's content 
    if(match.Groups[4].Success) 
    { 
     var value = match.Groups[4].Value; 

     // The "Count" of the items will 
     // be in the third capture group 
     var count = int.Parse(match.Groups[3].Value); 

     for(int i=0;i<count;i++) 
     { 
      ls.Add(value); 
     } 

    } 
    else 
    { 
     // otherwise we know that square bracket input 
     // is in the first capture group 
     ls.Add(match.Groups[1].Value); 
    } 
} 

这里确定的次数所需要的数量是解决方案的工作小提琴:https://dotnetfiddle.net/4rQsDj

这里是输出:

[952,M] 
[782,M] 
[782] 
[373,M] 
[373,M] 
[1470] 
[352] 
[235] 
[234] 
[610] 
[610] 
[610] 
[380] 
[380] 
[380] 
[128] 
[127] 

如果你不想支架可通过改变正则表达式模式以摆脱他们和match.Groups[1].Valuematch.Groups[6].Value

这里是方括号工作的解决方案:https://dotnetfiddle.net/OQwStf

+0

这似乎是最好的RegEx解决方案。与我的解决方案相比,我会对性能感到好奇。 –

+0

@JonathanWood不知道,知道肯定有很大的字符串正则表现的问题,也许我会弄乱小提琴看看我是否注意到任何东西 – konkked

+0

在1K行文本,花了大约.08s,并在500采取.03秒,因此看起来像是线性增长,如果实现者编译它可能会提高性能 – konkked

1

正则表达式下面将处理这两种情况下:

(?:\{([^\[]+)){0,1}\[([^\]]+)\]\}{0,1} 

对于没有花括号为您的情况下比赛,第一场比赛将是空的。对于第二种情况,第一场比赛将包含您的重复次数。在这两种情况下,第二场比赛将包含实际数据。下面的链接将显示此工作的一个演示:

Regex Demo

但是请注意,你将不得不自己处理的重复中,使得使用正则表达式

1

,而你可能能够代码如果你的需求变得太复杂,它可能会缩短。所以下面的代码显示了我将采取的一般方法来实现这一点。它有点快而肮脏,但符合你的要求。

此外,我有一个parsing helper class,这将使这个代码更容易编写和更健壮。

string input = "[952,M] [782,M] [782] {2[373,M]} [1470] [352] [235] [234] {3[610]}{3[380]} [128] [127]"; 
int pos = 0; 

void Main() 
{ 
    while (pos < input.Length) 
    { 
     SkipWhitespace(); 
     if (pos < input.Length && input[pos] == '{') 
      ParseBrace(); 
     else if (pos < input.Length && input[pos] == '[') 
      ParseBracket(); 
    } 
} 

void SkipWhitespace() 
{ 
    while (pos < input.Length && char.IsWhiteSpace(input[pos])) 
     pos++; 
} 

void ParseBrace() 
{ 
    Debug.Assert(pos < input.Length && input[pos] == '{'); 
    int pos2 = input.IndexOf('[', pos + 1); 
    if (pos2 < 0) 
     pos2 = input.Length; 

    int count = int.Parse(input.Substring(pos + 1, pos2 - pos - 1)); 
    for (int i = 0; i < count; i++) 
    { 
     pos = pos2; 
     ParseBracket(); 
    } 

    pos2 = input.IndexOf('}', pos2 + 1); 
    if (pos2 < 0) 
     pos2 = input.Length; 

    pos = pos2 + 1; 
} 

void ParseBracket() 
{ 
    Debug.Assert(pos < input.Length && input[pos] == '['); 
    int pos2 = input.IndexOf(']', pos + 1); 
    if (pos2 < 0) 
     pos2 = input.Length; 
    Console.WriteLine(input.Substring(pos + 1, pos2 - pos - 1)); 
    pos = pos2 + 1; 
} 

输出示例:

952,M 
782,M 
782 
373,M 
373,M 
1470 
352 
235 
234 
610 
610 
610 
380 
380 
380 
128 
127