【问题标题】:How to split a string that contains square brackets with spaces in it如何拆分包含方括号和空格的字符串
【发布时间】:2021-07-30 15:27:12
【问题描述】:

我正在尝试拆分以下字符串:

"add galaxy [Milky way] elliptical 13.2B"

我想要实现的结果数组应该包含以下字符串:

"add","galaxy","Milky way","eliptical","13.2B"

我尝试使用 string.Split() 并尝试了不同的方法参数,但没有达到我想要的效果。

我怎样才能达到这个结果?

编辑: 我找到了一种使用正则表达式的方法。

@"[A-Za-z0-9.]+|(#.*?#)|(\[.*?\])"

【问题讨论】:

    标签: c# arrays string split format


    【解决方案1】:

    这是一个快速而肮脏的解决方案。不考虑多个空格或多个嵌套[[[]]] 的可能性。

    string[] split(string s)
    {
        List<string> list = new List<string>();
        int start = 0;
        int end = 0;
        bool inBlock = false;
    
        for(; end < s.Length; end++)
        {
            if (s[end] == '[')
            {
                inBlock = true;
                start++;
            }
            else if(s[end] == ']' && inBlock)
            {
                inBlock = false;
                list.Add(s.Substring(start, end - start));
                end++;
                start = end + 1;
            }
            else if(s[end] == ' ' && !inBlock)
            {
                list.Add(s.Substring(start, end - start));
                start = end + 1;
            }
        }
        if(end > start)
            list.Add(s.Substring(start, end - start));
    
        return list.ToArray();
    }
    

    【讨论】:

      【解决方案2】:

      你也可以使用Regex.Matches来代替split。

      (?<=\[)[^][]+(?=])|(?<=#)[^#]+(?=#)|[A-Za-z0-9.]+
      

      说明

      • (?&lt;=\[)[^][]+(?=])Assert [ 到左边,] 到右边,两者之间都匹配
      • |或者
      • (?&lt;=#)[^#]+(?=#) 断言 # 到左边,# 到右边并匹配所有的 o
      • |或者
      • [A-Za-z0-9.]+ 匹配字符类中列出的任何字符的 1+ 次

      查看regex demoC# demo

      例如

      string pattern = @"(?<=\[)[^][]*(?=])|(?<=#)[^#]*(?=#)|[A-Za-z0-9.]+";
      string input = @"add galaxy [Milky way] elliptical 13.2B #test test #";    
      String[] matches = Regex.Matches(input, pattern)
                 .Cast<Match>()
                 .Select(m => m.Value)
                 .ToArray();
      
      foreach (String m in matches)
      {
          Console.WriteLine(m);
      }
      

      输出

      add
      galaxy
      Milky way
      elliptical
      13.2B
      test test
      

      如果您希望使用 Regex.Split 获得该结果,您可以使用模式中的捕获组(在外部 # 或方括号内)将分隔符保留在输出中并删除数组中的空条目。

      string pattern = @"([A-Za-z0-9.]+)|#([^#]+)#|\[([^][]+)]";
      string input = @"add galaxy [Milky way] elliptical 13.2B #test test #";
      string[] items = Regex.Split(input, pattern).Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
      
      foreach (string item in items)
      {
          Console.WriteLine(item);
      }
      

      输出

      add
      galaxy
      Milky way
      elliptical
      13.2B
      test test
      

      查看regex demoC# demo

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-30
        • 1970-01-01
        • 1970-01-01
        • 2016-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多