c# - String contains regex

String contains regex
string-contains-regex.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>

<!DOCTYPE html>  
<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        //this section create string variables.
        string stringColors = "Red Orange Maroon Coral DarkCyan";
        string stringColorsWithSerial = "1.Red 2.Orange 3.Maroon 4.Coral 5.DarkCyan";

        Label1.Text = "string..................<br />";
        Label1.Text += "stringColors: " + stringColors;
        Label1.Text += "<br />stringColorsWithSerial: " + stringColorsWithSerial;

        //check string contains only letters and space character using regular expression
        Regex rex = new Regex("^[a-zA-Z ]+$");
        Boolean result = rex.IsMatch(stringColors);
        Boolean result2 = rex.IsMatch(stringColorsWithSerial);

        Label1.Text += "<br /><br />stringColors contains letters and space only? " + result.ToString();
        Label1.Text += "<br /><br />stringColorsWithSerial contains letters and space only? " + result2.ToString();
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - string contains regex</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - string contains regex
        </h2>  
        <hr width="550" align="left" color="Gainsboro" />  
        <asp:Label   
            ID="Label1"   
            runat="server"  
            Font-Size="Large"
            >  
        </asp:Label>  
        <br /><br />
        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="string contains regex"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples