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

Posted on 2008-10-14
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'

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.


using System;
using System.Text.RegularExpressions;
namespace retest
    class Program
        static void Main(string[] args)
            string incoming = @"UCS+21'
            string incomingTemplate = @"UCS+{UcmHeader}'
            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);

Open in new window

Question by:djcheeky
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
LVL 27

Expert Comment

ID: 22715973
LVL 27

Expert Comment

ID: 22715985
That would show the start index of the match, add the string length to that value to get the ending index.
LVL 27

Expert Comment

ID: 22715998
I guess you could always use the length property to add to the index property to get the ending index:
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

LVL 27

Accepted Solution

ddrudik earned 500 total points
ID: 22716037
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);

Author Comment

ID: 22718878
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.
LVL 27

Expert Comment

ID: 22720478
Glad I could help, thanks for the question and the points.

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to repeat the data 4 43
Selenium and Xpath 4 69
What!!!??? 5 62
web api controller with multiple posts that accept different objects 1 25
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…

751 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