tags:

views:

89

answers:

4
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"}.

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.

A: 

I think you're looking for 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:
(?<=<)[^>]*(?=>)

Donut
Not quite, this gives me one match, for "<something><or><other>". I want three matches, one for "something", one for "or", and one for "other".
david
@david I modified your Regex; try the example now. It should give you the results you want.
Donut
+1  A: 

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.

Ron Warholic
A: 

Use 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);
        }
    }
Chuck
A: 

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();
    }
  }
}
Alan Moore