【问题标题】:Get all strings between two chars in a string [duplicate]获取字符串中两个字符之间的所有字符串[重复]
【发布时间】:2019-01-08 06:40:35
【问题描述】:

我有一个这样的字符串:

{abc}@{defgh}mner{123}

如何将{} 之间的所有字符串作为数组或列表获取?

像这样:

{ {abc},{defgh},{123} }

【问题讨论】:

  • 你已经尝试过了吗?有几种方法可以做到这一点
  • 对捕获组使用正则表达式
  • 是的,我写了一个循环来获取 { 和 } 之间的字符串,但这不是最好的方法。 @shree.pat18
  • @radin 此问题已关闭作为副本,您的原始问题是。新增内容改变了问题的范围,因此最好打开一个新问题(通常)。在这种特殊情况下,这个问题在这里回答了您的确切需求:stackoverflow.com/questions/2778532/…

标签: c# string split


【解决方案1】:

使用正则表达式是一个不错的选择

    string str = "{abc}@{defgh}mner{123}";
    foreach (Match match in Regex.Matches(str,"{[^}]+}"))
    {
        Console.WriteLine(match.Value);
    }

【讨论】:

    【解决方案2】:

    使用 RegExr 等网站,您可以在代码中使用正则表达式之前轻松尝试:

    string str = "{abc}@{defgh}mner{123}";
    foreach (Match match in Regex.Matches(str,"(\{.+?\})"))
    {
        Console.WriteLine(match.Value);
    }
    

    https://regexr.com/460a3

    【讨论】:

      【解决方案3】:
      var input = "{abc}@{defgh}mner{123}";
      var pattern = @"\{(.+?)\}";
      
      var matches = Regex.Matches(input, pattern);
      IList<string> output = new List<string>();
      foreach (Match match in matches)
          output.Add(match.Groups[0].Value);
      

      Fiddle Result


      简单版

      var input = "{abc}@{defgh}mner{123}";
      var pattern = @"\{(.+?)\}";
      
      var matches = Regex.Matches(input, pattern);
      IList<string> output = matches.Cast<Match>().Select(x => x.Groups[0].Value).ToList();           
      output.Dump();  
      

      Fiddle Result

      【讨论】:

        【解决方案4】:

        你可以使用正则表达式

        var str = "{abc}@{defgh}mner{123}";
        var regex = new Regex(@"({\w+})",RegexOptions.Compiled);
        var result = regex.Matches(str).Cast<Match>().Select(x=>x.Value);
        

        结果是IEnumerable&lt;string&gt; OP 中要求的

        输出(结果的值)

        {abc} 
        {defgh} 
        {123} 
        

        【讨论】:

          【解决方案5】:

          使用 Regex 并使用 LINQ 将它们转换为列表

          var l = new Regex(@"\{(\w+)\}")
                  .Matches("{abc}@{defgh}mner{123}l")
                  .Cast<Match>()
                  .Select(m => m.Groups[0].Value)
                  .ToList();
          

          它是如何工作的:

          正则表达式 {(\w+)} 表示:

          • {:查找{
          • (:开始捕获数据组
          • \w+:匹配一个或多个单词字符(a到z,0到9)
          • ): 捕获结束
          • ): 找一个}

          这将找到大括号之间的所有文本

          正则表达式会给出一个 MatchCollection

          我们必须使用 Cast 把它变成 linq 可以查询的东西

          We .Select items from the collection,m是单个item,m.Groups[0].Value是组捕获的文本,大括号之间

          .ToList 返回一个列表中的所有文本

          【讨论】:

            【解决方案6】:

            你可以试试下面的代码,它不使用正则表达式,所以你不需要知道它!

            static void Main(string[] args)
            {
              string s = "{abc}@{defgh}mner{123}";
              int i1, i2 = 0;
              while ((i1 = s.IndexOf('{', i2)) >= 0)
              {
                i2 = s.IndexOf('}', i1);
                // Here you can add Substring result to some list or assign it to a variable...
                Console.WriteLine(s.Substring(i1 + 1, i2 - i1 - 1));
              }
            }
            

            【讨论】:

              【解决方案7】:
              using System;
              using System.Text.RegularExpressions;
              
              public class Example
              {
                public static void Main()
                {
                 string pattern = @"{([A-Za-z0-9\-]+)}" ; 
                 string input = "{abc}@{defgh}mner{123}";
                 MatchCollection matches = Regex.Matches(input, pattern);
              
                 foreach (Match match in matches)
                 {
                   Console.WriteLine(match.Groups[1].Value);
                 }
                 Console.WriteLine();
                }
              }
              

              输出:

              abc

              defgh

              123

              您可以查看代码的在线执行情况: http://tpcg.io/uuIxo1

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2022-12-13
                • 1970-01-01
                • 2012-12-28
                • 2015-10-09
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多