Blog Archives

Quick Regex Reference

Commonly Used:

 
 private bool ValidTLD(string tld)
    {
        string[] validtlds = {"com","org","net","edu","gov","mil","biz","info","mobi","name","aero","asia","jobs","museum"};
        if (validtlds.Contains(tld.ToLower().Replace(".",""))) {
            return true;
        }

        return false;
    }

if (!ValidTLD(System.Text.RegularExpressions.Regex.Match(tbNewEmailAddress.Text, @"^\w+([-+.']\w+)*@.*(\.\w+)$").Groups[2].Value))
        {
            hlnkReturn.Visible = false;
            tblSuccess.Visible = false;
            tblError.Visible = true;
            pnlMessages.Visible = true;
            lblErrorMessage.Visible = true;
            lblErrorMessage.Text = "Invalid domain specified. Please contact support if you continue to receive this message.";   
        }

Get Top Level Domain of Email:

//ex. for ronniediaz@ronniediaz.com or ronniediaz@test.ronniediaz.com returns ".com"
string regextld = @"^\w+([-+.']\w+)*@.*(\.\w+)$";
if (System.Text.RegularExpressions.Regex.IsMatch(tbNewEmailAddress.Text, regextld))
        {
            System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(tbNewEmailAddress.Text, regextld);
                return m.Groups[2].Value;
        }