Share via

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#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

  1. Minxin Yu 13,516 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' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.