[英]Regex match multiple substrings inside a string
所以我有一个字符串,其中包含多次出现的子字符串。 所有这些字符串都具有以下格式: <c@=someText>Content<c>
例:
This combination of plain text and <c=@flavor> colored text<c> is valid. <c=@warning>Multiple tags are also valid.<c>
我想通过正则表达式提取每个子字符串。 但是,如果我使用以下正则表达式<c=@.+?(?=>)>.*<c>
它匹配从第一个<c...
到最后一个<c>
。 我想要的是将每个子字符串作为一项。 我该怎么做,如果我不能用正则表达式来做,那是实现我的目标的最佳方法。
您可以使用命名的捕获组以及前瞻性和后瞻性来获取“类型”和“文本”:
var pattern = @"(?<=<c=@)(?<type>[^>]+)>(?<text>.+?)(?=<c>)";
var str = @"This combination of plain text and <c=@flavor> colored text<c> is valid. <c=@warning>Multiple tags are also valid.<c>";
foreach (Match match in Regex.Matches(str, pattern))
{
Console.WriteLine(match.Groups["type"].Value);
Console.WriteLine(match.Groups["text"].Value);
Console.WriteLine();
}
输出:
flavor
colored text
warning
Multiple tags are also valid.
图案:
(?<=<c=@) :
寻找<c=@
(?<type>[^>]+)> :
抓取所有内容,直到>
,将其称为type
(?<text>.+?) :
抓取所有内容直到超前,将其称为text
(?=<c>) :
找到<c>
时停止
string input = @"This combination of plain text and <c=@flavor> colored text<c> is valid. <c=@warning>Multiple tags are also valid.<c>";
var matches = Regex.Matches(input, @"<c=@(.+?)>(.+?)<c>")
.Cast<Match>()
.Select(m => new
{
Name = m.Groups[1].Value,
Value = m.Groups[2].Value
})
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.