How to extract a substring from a .NET RegEx?

C#.NetRegex

C# Problem Overview


I have an XML file containing one (or more) key/value pairs. For each of these pairs I want to extract the value which is a two-byte hex value.

So the XML contains this snippet:

<key>LibID</key><val>A67A</val>

Which I can match using the following expression, with the ID in parenthesis.

Match match = Regex.Match(content, @"<key>LibID</key><val>([a-fA-F0-9]{4})</val>");

if (match.Success)
{
  Console.WriteLine("Found Match for {0}\n", match.Value);
  Console.WriteLine("ID was {0}\n", "Help me SO!");
}

How can I change the last part so it returns the ID from the match?

Cheers!

C# Solutions


Solution 1 - C#

I think you want

match.Groups[1].Value

(As Dillie-O points out in the comments, it's group 1 because group 0 is always the whole match.)

Short but complete test program:

using System;
using System.Text.RegularExpressions;

class Program
{
  static void Main()
  {
    Regex regex = new Regex("<key>LibID</key><val>([a-fA-F0-9]{4})</val>");
    Match match = regex.Match("Before<key>LibID</key><val>A67A</val>After");
       
    if (match.Success)
    {
      Console.WriteLine("Found Match for {0}", match.Value);
      Console.WriteLine("ID was {0}", match.Groups[1].Value);
    }      
  }
}

Output:

Found Match for <key>LibID</key><val>A67A</val>
ID was A67A

Solution 2 - C#

Add a grouping construct to your expression ...

<key>(?<id>LibID)</key><val>([a-fA-F0-9]{4})</val>

That will capture the ID. But, you need to put the correct format in your expression for the actual ID, because your regex will only capture "LibID" litterally.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrew GrantView Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#JP AliotoView Answer on Stackoverflow