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:
0 comments:
Please let me know your view