【问题标题】:Extracting a substring between two characters in a string C#提取字符串中两个字符之间的子字符串C#
【发布时间】:2013-12-11 10:03:41
【问题描述】:

我有一个生成的字符串有点像

($AverAbandCalls$+$TotalInboundCalls$)*50+$TotalOutboundCalls$

我想获取 $ 符号之间的所有内容。我该如何进行?

我知道可以使用.Split 但这给出了在与弦乐打成一片之后的最终答案。

我想知道是否有更简单的方法。

【问题讨论】:

  • “但这给出了在与弦乐打成一片之后的最终答案。” ???
  • I want to get everythng that is between the $ signs -> string.Split() 适合你除非你有更多的要求......
  • 请注意,+)*50+ 也在“$ 符号之间”。

标签: c# string split substring


【解决方案1】:

使用 Regex(正则表达式)。这包含要匹配的模式的描述 - 在您的情况下,是一个美元符号、一些非美元符号字符和另一个美元符号。

在 .NET 中,正则表达式支持位于 System.Text.RegularExpressions 库中,因此您必须在代码中引用它。

这是一个简单的例子:

string pattern = "\$([^\$]*)\$";
var matches = Regex.Matches(input, pattern);

【讨论】:

  • 我收到一个错误,因为无法识别转义序列并且 $ 符号带有下划线。是吗?
【解决方案2】:

试试这个正则表达式:

(?:\$).*?(?:\$)

由于您使用的是 .NET,您也可以试试balancing groups

(?<open>\$).*?(?<final-open>\$)

例子:

var input = @"($AverAbandCalls$+$TotalInboundCalls$)*50+$TotalOutboundCalls$";
var reg = new Regex(@"(?<open>\$).*?(?<final-open>\$)");
var matches = reg.Matches(input).Cast<Match>()
    .Select(m=>m.Groups["final"].Value).ToList();

foreach (var item in matches)
{
    Console.WriteLine(item);
}

哪些输出:

AverAbandCalls
TotalInboundCalls
TotalOutboundCalls

【讨论】:

  • 正是我想要的。谢谢。
【解决方案3】:
 string input = "($AverAbandCalls$+$TotalInboundCalls$)*50+$TotalOutboundCalls$";
 IEnumerable<string> matches =
        Regex.Matches(input, @"\$([^\$]+)\$")
        .OfType<Match>()
        .Select(m => m.Groups[1].Value);

此正则表达式使用“\$”匹配文字 $ 字符,然后使用“$”匹配一个或多个非$ 字符,然后再次使用“\$”匹配另一个$

它使用圆括号“([^\$]+)”将位包围起来,使其成为一个易于抓取的正则表达式组。

已经有一个默认的正则表达式组(即整个匹配),所以我们在索引 1 处获取组。

【讨论】:

    【解决方案4】:

    你可以这样做

    using System.Text.RegularExpressions;
    using System;
    public class Test
    {
            public static void Main(){
                    string s = "My name is $Dave$ and I am $18$ years old";
                    Regex r = new Regex(@"$(.+?)$");
                    MatchCollection mc = r.Matches(s);
                    Console.WriteLine("Name is " + mc[0].Groups[1].Value);
                    Console.WriteLine("Age is " + mc[1].Groups[1].Value);
            }
    }
    

    【讨论】:

      【解决方案5】:

      这是一个没有任何正则表达式的简单方法:

      string SubstringBetweenSymbols(string str, char preSymbol, char postSymbol)
          {
              int? preSymbolIndex = null;
              int? postSymbolIndex = null;
      
      
              for (int i = 0; i < str.Length; i++)
              {
                  if (i == 0 && preSymbol == char.MinValue)
                  {
                      preSymbolIndex = -1;
                  }
                  if (str[i] == preSymbol && !(preSymbolIndex.HasValue && preSymbol == postSymbol))
                  {
                      preSymbolIndex = i;
                  }
                  if (str[i] == postSymbol && preSymbolIndex.HasValue && preSymbolIndex != i)
                  {
                      postSymbolIndex = i;
                  }
                  if (i == str.Length - 1 && postSymbol == char.MinValue)
                  {
                      postSymbolIndex = str.Length;
                  }
      
      
      
                  if (preSymbolIndex.HasValue && postSymbolIndex.HasValue)
                  {
                      var result = str.Substring(preSymbolIndex.Value + 1, postSymbolIndex.Value - preSymbolIndex.Value - 1);
                      return result;
                  }
              }
      
              return string.Empty;
          }
      

      Pre 和 Post 符号可能是一个 char.MinValue,表示字符串的开头或字符串的结尾。

      【讨论】:

        猜你喜欢
        • 2013-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多