Finding the start and end character positions / indexes of a RegEx match.

Given the code below, when the RegEx matches the incoming string to the incoming template string, I would like to somehow determine the start and end character inde of the exact match.

Obviously in this case, start would be 1. But how do you calcualte the last character position  of the match, which in this case in the incoming string is a single quotation ( ' ) ??

Bare in mind that even though this example contains an exact match, it is possible that the incoming string might be:

string incoming = @"UCS+21'
UCD+14+3'
MORETEXT+nothing'
ANDMORE+goes+here'";

Which is why I can't just use a simple function like Right() or something similar. I need to actually get the last position of the matched item in the incoming string.

Thanks.

using System;
using System.Text.RegularExpressions;
namespace retest
{
    class Program
    {
        static void Main(string[] args)
        {
            string incoming = @"UCS+21'
UCD+14+3'";
            string incomingTemplate = @"UCS+{UcmHeader}'
UCD+{UcmHeader}+{UcmSummary}'";
            string repattern = Regex.Replace(Regex.Replace(Regex.Replace(incomingTemplate, @"\+", @"\+"), @"\{", @"(?<"), @"\}", @">.*?)");
            Regex reParse = new Regex(repattern);
            if (reParse.IsMatch(incoming))
            {
                Match m = reParse.Match(incoming);
                for (int gIdx = 1; gIdx < m.Groups.Count; gIdx++)
                {
                    Console.WriteLine("[" + reParse.GetGroupNames()[gIdx] + "] = " + m.Groups[gIdx].Value);
                }
            }
            Console.ReadLine();
        }
    }
}

Open in new window

djcheekyAsked:
Who is Participating?
 
ddrudikConnect With a Mentor Commented:
Console.WriteLine("[" + reParse.GetGroupNames()[gIdx] + "] = " + m.Groups[gIdx].Value);
Console.WriteLine("captures[0] value: " + m.Captures[0].Value);
Console.WriteLine("captures[0] index: " + m.Captures[0].Index);
Console.WriteLine("captures[0] length: " + m.Captures[0].Length);
0
 
ddrudikCommented:
That would show the start index of the match, add the string length to that value to get the ending index.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ddrudikCommented:
I guess you could always use the length property to add to the index property to get the ending index:
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.capture.length.aspx
0
 
djcheekyAuthor Commented:
Thanks ddrudik

As you can tell, I have broken up all my little regex issues into smaller problems and then posted them here on the forums, with which you have helped the most (so thanks again for that).

What I am going to do now is try and combine them into one process, which will more than likely lead to more gray hairs - but let's see how it goes :)

I appreciate all your help.
Regards
Paolo
0
 
ddrudikCommented:
Glad I could help, thanks for the question and the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.