Solved

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

Posted on 2008-10-14
6
580 Views
Last Modified: 2008-10-15
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

0
Comment
Question by:djcheeky
  • 5
6 Comments
 
LVL 27

Expert Comment

by:ddrudik
Comment Utility
0
 
LVL 27

Expert Comment

by:ddrudik
Comment Utility
That would show the start index of the match, add the string length to that value to get the ending index.
0
 
LVL 27

Expert Comment

by:ddrudik
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 27

Accepted Solution

by:
ddrudik earned 500 total points
Comment Utility
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
 

Author Comment

by:djcheeky
Comment Utility
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
 
LVL 27

Expert Comment

by:ddrudik
Comment Utility
Glad I could help, thanks for the question and the points.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now