Extract string between two characters using SubString and RegularExpression in C#

Last Reply 7 months ago By kalpesh

Posted 7 months ago

My String is

for example

"xyz abc" <xyzabc2571997@gmail.com>

i want xyzabc2571997@gmail.com

 

ex

"riya vyas" <riyavyas1997@gmail.com>

i want riyavyas1997@gmail.com

in c# website

You are viewing reply posted by: kalpesh 7 months ago.
Posted 7 months ago Modified on 7 months ago

Try by below code if you string contain only one email details.

string testText = @"""riya vyas"" <riyavyas1997@gmail.com>";
int startindex = testText.IndexOf('<');
int Endindex = testText.IndexOf('>');
string outputstring = testText.Substring(startindex + 1, Endindex - startindex - 1);

If its multiple then you can store it in string array variable like below.

string input = @"""xyz abc"" <xyzabc2571997@gmail.com>""riya vyas"" <riyavyas1997@gmail.com>";
string[] testing = Regex.Matches(input, @"\<(.+?)\>")
                            .Cast<Match>()
                            .Select(s => s.Groups[1].Value).ToArray();