Main Page
 The gatekeeper of reality is
 quantified imagination.

Stay notified when site changes by adding your email address:

Your Email:

Bookmark and Share
Email Notification
.Net Regular Expressions
Purpose
The purpose of this tutorial is to show some fairly simple examples of using Regular Expressions to pattern match string data. Depending on the type of project you use in Visual Studio, you may not need to use regular expressions at all.

For example, under ASP.NET MVC with a FORM, you can apply a variety of "decorators" to class members (which may be an input box in the FORM, for example) in the Model to indicate whether it is [Required]. You can perform value validation as well by simply adding more decorators such as "[StringLength(max-length, ErrorMessage="String too long.")]" to that member. This capability is possible by adding the ComponentModel and DataAnnotations to the Model; you do not need to employ a front-end framework or process to attempt client-side validation of the FORM (which can be bypassed) or setup a complex server-side process to validate FORM data (such as using Regular Expressions). Instead, the validation is automatically handled by .Net based on the Model decorators you specified for various FORM fields and, if .Net finds validation has failed, it will automatically display a message in the FORM (through use of Bootstrap, a responsive design front-end framework for HTML and CSS). The only other thing you would be concerned with is adding a few lines to the ActionResult Controller associated to the FORM (called when the form is submitted, in order to handle the FORM data) to tell .Net to prevent the FORM submission until all validation passes, such as "if (!ModelState.IsValid) { /* Form validation failed. Inform user */ return View(form-data); } else { /* Form validated. Continue... */ }".

Regular Expression Examples:

using System.Text.RegularExpressions;

private static Boolean isNumeric(string input)
{
	bool result = false;
	if (Regex.IsMatch(input, "^[0-9]*$")) { result = true; }
	else { result = false; }
	return result;
}
private static Boolean isAlphanumeric(string input)
{
	bool result = false;
	if (Regex.IsMatch(input, "^[a-zA-Z0-9]*$")) { result = true; }
	else { result = false; }
	return result;
}
private static Boolean isAlphanumericWithPeriod(string input)
{
	bool result = false;
	if (Regex.IsMatch(input, "^[-_a-zA-Z0-9.]*$")) { result = true; }
	else { result = false; }
	return result;
}
private static Boolean isAlphanumericWithNameChars(string input)
{
	bool result = false;
	if (Regex.IsMatch(input, "^[a-zA-Z0-9 .',]*$")) { result = true; }
	else { result = false; }
	return result;
}
private static Boolean isValidEmail(string input)
{
	bool result = false;
	if (Regex.IsMatch(input, "^[-_a-zA-Z0-9.@]*$")) { result = true; }
	else { result = false; }
	return result;
}
private static Boolean isAlphanumericWithCommonChars(string input)
{
	bool result = false;
	if (Regex.IsMatch(input, "^[\\b-_a-zA-Z0-9]*$")) { result = true; }
	else { result = false; }
	return result;
}
private static Boolean isAlphanumericWithAdditionalChars(string input)
{
	bool result = false;
	if (Regex.IsMatch(input, "^[\\b-_a-zA-Z0-9 :;>.()!,]*$")) { result = true; }
	else { result = false; }
	return result;
}


About Joe