Regular Expression tester - ASP.NET
I've posted code in ColdFusion and JavaScript for a regular expression tester. Since I'm looking at ASP.NET development ... here's one built on ASP.NET (3.5, but it should run fine on 2.0), in C#.
RegularExpression.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RegularExpression.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ASP.NET 3.5 Regular Expression tester</title>
<style type="text/css">
form {
margin:0;
padding:0;
}
form label, .label_kludge {
display:block;
float:left;
width:180px;
padding:0;
margin:5px 0 0;
text-align:right;
}
form input, form textarea, form select {
width:200px;
margin:5px 0 0 10px;
}
textarea {
overflow:auto;
}
form br {
clear:left;
}
</style>
</head>
<body>
<h1>ASP.NET 3.5 Regular Expression Tester</h1>
<form id="form1" runat="server">
<div>
<asp:Label ID="lbl_RegularExpression" runat="server" Text="Regular Expression:"
AssociatedControlID="RegularExpression"></asp:Label>
<asp:TextBox ID="RegularExpression" runat="server" Width="350"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="RegularExpression" Display="Dynamic"
ErrorMessage="You must enter a regular expression." SetFocusOnError="True">You
must enter a regular expression.</asp:RequiredFieldValidator>
<br />
<asp:Label ID="lbl_TestString" runat="server" Text="Test String:"
AssociatedControlID="TestString"></asp:Label>
<asp:TextBox ID="TestString" runat="server" Width="350px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
Display="Dynamic"
ErrorMessage="You must enter a string to test the regular expression against."
ControlToValidate="TestString">You
must enter a string to test the regular expression against.</asp:RequiredFieldValidator>
<br />
<asp:Label ID="lbl_CaseSensitive" runat="server" Text="Case Sensitive:"
AssociatedControlID="CaseSensitive"></asp:Label>
<asp:CheckBox ID="CaseSensitive" runat="server" />
<br />
<br />
<span class="label_kludge"> </span><asp:Button ID="btn_Test"
runat="server" Text="Test" onclick="btn_Test_Click" />
<br />
<asp:Label ID="lbl_Results" runat="server"></asp:Label>
<br />
</div>
</form>
</body>
</html>
RegularExpression.aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e) {
if (this.IsPostBack) {
Boolean MatchCheck;
if (this.CaseSensitive.Checked) {
MatchCheck = Regex.IsMatch(this.TestString.Text, this.RegularExpression.Text);
} else {
MatchCheck = Regex.IsMatch(this.TestString.Text, this.RegularExpression.Text, RegexOptions.IgnoreCase);
}
if (MatchCheck) {
this.lbl_Results.Text = "Result: match found";
} else {
this.lbl_Results.Text = "Result: no match found";
}
}
}
protected void btn_Test_Click(object sender, EventArgs e) {
}
}
See it in action!
Want to see it in action? You can do so on JamesRSkemp.com.
Search
Links of Note
Support This Site
If my blog was helpful to you, then please consider visiting my Amazon Wishlist.