简体   繁体   English

如何从.NET RegEx提取子字符串?

[英]How to extract a substring from a .NET RegEx?

I have an XML file containing one (or more) key/value pairs. 我有一个包含一个(或多个)键/值对的XML文件。 For each of these pairs I want to extract the value which is a two-byte hex value. 对于这些对中的每对,我都希望提取一个为两个字节的十六进制值的值。

So the XML contains this snippet: 因此,XML包含以下代码段:

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

Which I can match using the following expression, with the ID in parenthesis. 我可以使用以下表达式将其与括号中的ID匹配。

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? 如何更改最后一部分,以便它从匹配项中返回ID?

Cheers! 干杯!

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.) (正如Dillie-O在评论中指出的,它是组1,因为组0始终是整个比赛。)

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

Add a grouping construct to your expression ... 分组构造添加到表达式中...

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

That will capture the ID. 这将捕获ID。 But, you need to put the correct format in your expression for the actual ID, because your regex will only capture "LibID" litterally. 但是,您需要在表达式中输入实际ID的正确格式,因为您的正则表达式只会乱码捕获“ LibID”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM