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

Chand
 
on Feb 27, 2018 12:00 AM
47577 Views

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

Download FREE API for Word, Excel and PDF in ASP.Net: Download
nagaraju60
 
on Feb 27, 2018 12:25 AM
kalpesh
 
on Feb 27, 2018 12:29 AM
on Feb 27, 2018 12:35 AM

Hi Chand,

 Please refer below Sample.

HTML

<asp:Label id="lblEmail" runat="server" />
<hr />
<asp:Label id="lblEmails" runat="server" />

Namespace

C#

using System.Text.RegularExpressions;

VB.Net

Imports System.Text.RegularExpressions

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string testText = @"riya vyas"" <riyavyas1997@gmail.com>";
        int startindex = testText.IndexOf('<');
        int endindex = testText.IndexOf('>');
        string outputstring = testText.Substring(startindex + 1, endindex - startindex - 1);
        lblEmail.Text = outputstring;

        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();
        lblEmails.Text = string.Join(", ", testing);
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim testText As String = "riya vyas"" <riyavyas1997@gmail.com>"
        Dim startindex As Integer = testText.IndexOf("<"c)
        Dim endindex As Integer = testText.IndexOf(">"c)
        Dim outputstring As String = testText.Substring(startindex + 1, endindex - startindex - 1)
        lblEmail.Text = outputstring
        Dim input As String = """xyz abc"" <xyzabc2571997@gmail.com>""riya vyas"" <riyavyas1997@gmail.com>"
        Dim testing As String() = Regex.Matches(input, "\<(.+?)\>").Cast(Of Match)().Select(Function(s) s.Groups(1).Value).ToArray()
        lblEmails.Text = String.Join(", ", testing)
    End If
End Sub

Screenshot