C# Regex to extract substrings from a lengthy comment

Karthikeyan 20 Reputation points
2023-09-26T11:50:52.6666667+00:00

Hi there,

"Created by @< MyName & gt; for the #ID-876 and reviewed by @< HisName & gt;"

I want to extract the string which is inbetween @< and & gt; from a lengthy comments using regex or other options, please guide me on this

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Minxin Yu 13,501 Reputation points Microsoft External Staff
    2023-09-26T13:01:57.52+00:00

    Hi, @Karthikeyan

    I speculate that the & gt symbol in the problem is a web page parsing error, it should be >

    using System;
    using System.Text.RegularExpressions;
    
    class Program
    {
        static void Main()
        {
            string input = "Created by @< MyName >; for the #ID-876 and reviewed by @< HisName >;";
    
          
            string pattern = @"@<([^>]*)>";
    
          
            MatchCollection matches = Regex.Matches(input, pattern);
    
            if (matches.Count > 0)
            {
                foreach (Match match in matches)
                {
                   
                    string extractedText = match.Groups[1].Value;
                    Console.WriteLine("text: " + extractedText);
                }
            }
            else
            {
                Console.WriteLine("not found");
            }
        }
    }
    
    

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.