Friday, 29 October 2021

Extracting String Between Two Characters By Regex in C# (Asp.Net Core, MVC, Console)

10/29/2021 - By Pranav Singh 0

This article will show you how you can extract string between two characters using regex in C#. This you can use in asp.net core, MVC and console application. This will help you to get string between character using regex c#. It will cover following topics.


  • Get string between two special characters regex
  • How to extract a string between two delimiters
  • Regex get string between two brackets
  • Regex find string between two characters
  • Replace string between two special characters
  • How to split string between two characters in c#
  • Extract string between two characters

 

Suppose you have a string  


You visited following country first #India#, Second country #Nepal#, Third Country #United Kingdom#.

 

Now we will write code to extract all the country names. For this create a new console application and add the below code.


static void Main(string[] args)

        {

            string userinput = "You visited following country first #India#, Second country #Nepal#, Third Country #United Kingdom#.";

            //For getting output in comman seprated string

            var outputstring = String.Join(";", Regex.Matches(userinput, @"\#(.+?)\#")

                                    .Cast<Match>()

                                    .Select(m => m.Groups[1].Value));

            //for getting data in list format

            var outputlist = String.Join(";", Regex.Matches(userinput, @"\#(.+?)\#")

                                    .Cast<Match>()

                                    .Select(m => m.Groups[1].Value)).Split(';');

        }

 

In above code we are having two part first part to get output as string


string userinput = "You visited following country first #India#, Second country #Nepal#, Third Country #United Kingdom#.";

            //For getting output in comman seprated string

            var outputstring = String.Join(";", Regex.Matches(userinput, @"\#(.+?)\#")

                                    .Cast<Match>()

                                    .Select(m => m.Groups[1].Value));

 

Output as List:

  //for getting data in list format

            var outputlist = String.Join(";", Regex.Matches(userinput, @"\#(.+?)\#")

                                    .Cast<Match>()

                                    .Select(m => m.Groups[1].Value)).Split(';');

 

Now run the application and check the output.

Output as String:

Extracting String Between Two Characters By Regex in C# (Asp.Net Core, MVC, Console)


Output as List:
 

 

Extracting String Between Two Characters By Regex in C# (Asp.Net Core, MVC, Console)

 

Tags:
About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top