Water Cooler v2
Water Cooler v2

Reputation:

Returning ordinal positions of regex matches in the original string

For fun, I'm developing a utility that will take an input string and a regex and will color highlight all matches of the regex in the original string, alike the Find function in Firefox or IE 8, and some popular browsers.

I am using C#. Does any member of the System.Text.RegularExpressions namespace return the ordinal positions of the matches in the original string? I also need their lengths.

Is there a way to get this information?

Upvotes: 1

Views: 254

Answers (1)

ba__friend
ba__friend

Reputation: 5903

var rgx = new Regex("my pattern");
var matches = rgx.Matches("my string");

foreach (Match m in matches)
{
    Console.WriteLine(String.Format("pos: {0}, len: {1}", m.Index, m.Length));
}

Upvotes: 3

Related Questions