Search This Blog

Tuesday, 9 April 2013

find email address in text file using c#


  string data = File.ReadAllText("filepath"); //read File
        //instantiate with this pattern
        Regex emailRegex = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase);
        //find items that matches with our pattern
        MatchCollection emailMatches = emailRegex.Matches(data);


        foreach (Match emailMatch in emailMatches)
        {         
            String AnchorLink;
            AnchorLink = "<" + "a href=" + emailMatch.Value + " style="+"text-decoration:underline"+">" + emailMatch.Value + "<a>";
            lblId.Text += data.Replace(emailMatch.Value, AnchorLink);                   
        }      

No comments:

Post a Comment