简体   繁体   English

正则表达式匹配字符串中的多个子字符串

[英]Regex match multiple substrings inside a string

So I have this one string, which contains multiple occurrences of a substring. 所以我有一个字符串,其中包含多次出现的子字符串。 All of these strings have the following format: <c@=someText>Content<c> 所有这些字符串都具有以下格式: <c@=someText>Content<c>

Example: 例:

This combination of plain text and <c=@flavor> colored text<c> is valid. <c=@warning>Multiple tags are also valid.<c>

I want to extract each of the substrings via regex. 我想通过正则表达式提取每个子字符串。 However if I use the following regex <c=@.+?(?=>)>.*<c> It matches everything from the first <c... to the last <c> . 但是,如果我使用以下正则表达式<c=@.+?(?=>)>.*<c>它匹配从第一个<c...到最后一个<c> What I want is each of those substrings as one item. 我想要的是将每个子字符串作为一项。 How can I do this and if I can't do it with regex, what would be the best way to achieve my goal. 我该怎么做,如果我不能用正则表达式来做,那是实现我的目标的最佳方法。

You can use named capture groups, along with lookaheads and lookbehinds, to grab the 'type' and 'text': 您可以使用命名的捕获组以及前瞻性和后瞻性来获取“类型”和“文本”:

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();
}

output: 输出:

flavor
 colored text

warning
Multiple tags are also valid.

pattern: 图案:

(?<=<c=@) : Look for <c=@ (?<=<c=@) :寻找<c=@

(?<type>[^>]+)> : Grab everything until a > , call it type (?<type>[^>]+)> :抓取所有内容,直到> ,将其称为type

(?<text>.+?) : Grab everything until the lookahead, call it text (?<text>.+?) :抓取所有内容直到超前,将其称为text

(?=<c>) : Stop when you find a <c> (?=<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.

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