Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取字符串中两个字符之间的所有字符串_C#_String_Split - Fatal编程技术网

C# 获取字符串中两个字符之间的所有字符串

C# 获取字符串中两个字符之间的所有字符串,c#,string,split,C#,String,Split,我有这样一个字符串: {abc}@{defgh}mner{123} 如何将{和}之间的所有字符串作为数组或列表获取 像这样: {{abc}、{defgh}、{123}使用正则表达式是一个不错的选择 string str = "{abc}@{defgh}mner{123}"; foreach (Match match in Regex.Matches(str,"{[^}]+}")) { Console.WriteLine(match.Value);

我有这样一个字符串:

{abc}@{defgh}mner{123}

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

像这样:


{{abc}、{defgh}、{123}

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

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

使用像RegExr这样的站点,在代码中使用regex之前,您可以很容易地试用它:

string str = "{abc}@{defgh}mner{123}";
foreach (Match match in Regex.Matches(str,"(\{.+?\})"))
{
    Console.WriteLine(match.Value);
}
var input=“{abc}@{defgh}mner{123}”;
变量模式=@“\{(+?)\}”;
var matches=Regex.matches(输入,模式);
IList输出=新列表();
foreach(匹配中的匹配)
output.Add(match.Groups[0].Value);


简单版本

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();  
var input=“{abc}@{defgh}mner{123}”;
变量模式=@“\{(+?)\}”;
var matches=Regex.matches(输入,模式);
IList output=matches.Cast().Select(x=>x.Groups[0].Value.ToList();
output.Dump();
您可以使用正则表达式

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

使用正则表达式并使用LINQ将它们转换为列表

var l = new Regex(@"\{(\w+)\}")
        .Matches("{abc}@{defgh}mner{123}l")
        .Cast<Match>()
        .Select(m => m.Groups[0].Value)
        .ToList();
var l=new Regex(@“\{(\w+\}”)
.Matches(“{abc}@{defgh}mner{123}l”)
.Cast()
.Select(m=>m.Groups[0]。值)
.ToList();
工作原理:

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

  • {:找一个{
  • (:开始捕获数据组
  • \w+:匹配一个或多个单词字符(a到z,0到9)
  • ):捕获结束
  • ):查找a}
这将在花括号之间找到所有文本

正则表达式将提供一个匹配集合

我们必须使用Cast将其转换为linq可以查询的内容

We.从集合中选择项,m是单个项,m.Groups[0]。值是组捕获的文本,位于花括号之间


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

您可以尝试以下代码,它不使用正则表达式,因此您不需要知道它

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));
  }
}
输出:

abc

德夫

123

您可以检查代码的联机执行情况:

您已经尝试过了吗?有几种方法可以做到这一点使用带有捕获组的正则表达式,我为{和}之间的get字符串编写了一个循环,但这不是最好的方法。@shree.pat18和的可能副本。@radin此问题已作为副本关闭,与您原来的问题相同。新添加的内容更改了问题的范围,因此,您只需打开一个新问题(一般情况下)就更好了。在这种情况下,此问题正好满足您的需要:
var l = new Regex(@"\{(\w+)\}")
        .Matches("{abc}@{defgh}mner{123}l")
        .Cast<Match>()
        .Select(m => m.Groups[0].Value)
        .ToList();
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));
  }
}
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();
  }
}