简体   繁体   English

C#正则表达式:返回结果集合

[英]C# Regex: returning a collection of results

string: "<something><or><other>"
regex pattern: "<(\w+)><(\w+)><(\w+)>"

How do I make a regex call that returns to me a collection of results containing everything between the parentheses? 如何进行正则表达式调用以返回包含括号之间所有内容的结果集合? For example, I would want a result set of {"something", "or", "other"}. 例如,我想要一个结果集{“ something”,“ or”,“ other”}。

For bonus points, what is this called? 对于奖励积分,这叫什么? Captures? 捕获? Capturing groups? 捕捉团体? Some kind of templating? 某种模板? I feel like if I knew the proper terminology I could search for it. 我觉得如果我知道合适的术语,就可以搜索它。

Thank you. 谢谢。

They're typically referred to as capture groups, and you can get the captures as shown: 它们通常称为捕获组,您可以获取捕获,如下所示:

Regex regex = new Regex(@"some (pattern)");
Match m = regex.Match("some pattern");

foreach (Capture c in m.Groups)  {
  Console.WriteLine(c.Value); // write the value to the console "pattern"
}

m.Groups.Count will let you know how many groups matched, m.Groups[0] will always be the full match text. m.Groups.Count将让您知道匹配的组数, m.Groups[0]将始终是完全匹配的文本。

I think you're looking for MatchCollection : 我认为您正在寻找MatchCollection

Represents the set of successful matches found by iteratively applying a regular expression pattern to the input string. 表示通过对输入字符串迭代应用正则表达式模式而找到的一组成功匹配项。

Example: 例:

string input = "<something><or><other>";
string pattern = "(?<=<)[^>]*(?=>)";

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

foreach (Match match in matches)
{
   Console.WriteLine(match.ToString()); // do something with match here
}

Edit: Changed regex from this: 编辑:从此更改正则表达式:
<(\\w+)><(\\w+)><(\\w+)>
to this: 对此:
(?<=<)[^>]*(?=>)

Use MatchCollection. 使用MatchCollection。 EDIT: Forgot to change the regex to what you were requesting. 编辑:忘记将正则表达式更改为您要的内容。 The code below generates the following output: 下面的代码生成以下输出:

something 某物
or 要么
other 其他

Is that what you were looking for? 那是您要找的东西吗?

  static void Main(string[] args)
    {
        var str = "<something><or><other>";
        var re = new Regex(@"(\w+)");
        MatchCollection mc = re.Matches(str);

        foreach (Match m in mc)
        {
            Console.WriteLine(m.Value);
        }
    }

There are many ways to skin this cat: 有很多方法可以给这只猫换皮:

using System;
using System.Text.RegularExpressions;

namespace Test
{
  class Test
  {
    static void Main(string[] args)
    {
      string target = @"<something><or><other>";

      // One group, many matches
      Regex r1 = new Regex(@"<(\w+)>");
      MatchCollection mc = r1.Matches(target);
      foreach (Match m in mc)
      {
        Console.WriteLine(m.Groups[1].Value);
      }
      Console.WriteLine();

      // One match, many groups
      Regex r2 = new Regex(@"<(\w+)><(\w+)><(\w+)>");
      Match m2 = r2.Match(target);
      if (m2.Success)
      {
        foreach (Group g in m2.Groups)
        {
          Console.WriteLine(g.Value);
        }
      }
      Console.WriteLine();

      // One group, one match, many captures
      Regex r3 = new Regex(@"(?:<(\w+)>)+");
      Match m3 = r3.Match(target);
      if (m3.Success)
      {
        foreach (Capture c in m3.Groups[1].Captures)
        {
          Console.WriteLine(c.Value);
        }
      }
      Console.WriteLine();

      // Many matches, no groups, no captures
      Regex r4 = new Regex(@"(?<=<)\w+(?=>)");
      foreach (Match m in r4.Matches(target))
      {
        Console.WriteLine(m.Value);
      }
      Console.ReadLine();
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM