Returning the ordinal position of regex matches in the originalstring

S

Sathyaish

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?
 
J

Jesse Houwing

Hello Sathyaish,
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?

Use Regex.Match/Match.NextMatch or Regex.Matches to get all the Match object
for the regex, then use the Index and Length properties of the Match object.

From the MSDN documentation:

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.match_properties.aspx
Index The position in the original string where the first character of the
captured substring was found. (Inherited from Capture.)
Length The length of the captured substring. (Inherited from Capture.)
 
J

Jesse Houwing

Hello Sathyaish,
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?

Use Regex.Match/Match.NextMatch or Regex.Matches to get all the Match object
for the regex, then use the Index and Length properties of the Match object.

From the MSDN documentation:

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.match_properties.aspx
Index The position in the original string where the first character of the
captured substring was found. (Inherited from Capture.)
Length The length of the captured substring. (Inherited from Capture.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top