C# 如何从 .NET RegEx 中提取子字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/738543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to extract a substring from a .NET RegEx?
提问by Andrew Grant
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.
我有一个包含一个(或多个)键/值对的 XML 文件。对于这些对中的每一个,我想提取一个两字节的十六进制值。
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!
干杯!
采纳答案by Jon Skeet
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
回答by JP Alioto
Add a grouping constructto 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.
这将捕获ID。但是,您需要在表达式中为实际 ID 放置正确的格式,因为您的正则表达式只会在字面上捕获“LibID”。