简体   繁体   中英

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>

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> . 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=@

(?<type>[^>]+)> : Grab everything until a > , call it type

(?<text>.+?) : Grab everything until the lookahead, call it text

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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