Jasinski Technical Wiki


Home Page
All Pages

Quick Search
Advanced Search »

Contributor Links

Create a new Page
File Management
Your Profile

Other Wiki Sections



Regular Expression Samples - .Net Framework

Modified on Sat, Feb 09, 2013, 9:07 AM by Administrator Categorized as Regular Expressions, ┬ĚNet Framework


  • To use regular expressions, be sure to include using System.Text.RegularExpressions; in your file header.
  • Regular expressions with a Passed note in the SDL column have successfully passed testing in the SDL Regex Fuzzer (available here) when run for All ASCII Characters and 500 iterations.

Item TemplateSDLRegular Expression
Date MM/DD/YYYY ^(\d{2}/\d{2}/\d{4})?$
Time h:mmtt1  ^(1[0-2]|\d)\:\d{2}\s{0,}[APap][Mm]$
Phone Number xxx-xxx-xxxxPassed^[2-9]\d{2}\-\d{3}\-\d{4}$
xxx-xxx-xxxx OR
(xxx) xxx-xxxx
Email Address  Passed^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$
Guidhhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh ^\{{0,1}[A-Fa-f0-9]{8}\-[A-Fa-f0-9]{4}\-[A-Fa-f0-9]{4}\-[A-Fa-f0-9]{4}\-[A-Fa-f0-9]{12}\}{0,1}$
Object Method   var.([A-Za-z0-9]*)[^A-Za-z0-9]
8.3 File Name, strict2   ^[A-Za-z][A-Za-z0-9_]{0,7}\.[A-Za-z0-9_]{0,3}$
8.3 File name, lenient2   ^[A-Za-z][^ \"<>|:*?\\/]{0,7}\.[^ \"<>|:*?\\/]{0,3}$
URL   ^(?#Protocol)(?:(?:ht|f)tp(?:s?)\:\/\/|~/|/)?(?#Username:Password)(?:\w+:\w+@)?(?#Subdomains)(?:(?:[-\w]+\.)+(?#TopLevel Domains)(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))(?#Port)(?::[\d]{1,5})?(?#Directories)(?:(?:(?:/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|/)+|\?|#)?(?#Query)(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?#Anchor)(?:#(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)?$

1 - Validates hour is between 1 and 12; can have space before meridian indicator (am/pm).
2 - 8.3 file name, strict = Allowing first char alpha, the rest alphanumeric (or underscore)

Using Back References

Regex r = new RegEx("Cells\(([A-Za-z]*))");
MatchCollection mc = r.Matches("Cells(uxIdColumn)")
string result = mc[0].Groups[1].Value
// result now = "uxIdColumn"

Finding an Exact Match

//Note: MUST wrap the regular expression in parentheses (i.e., use groups)
regex.Matches(testString).Item(0).Groups(1).Value = testString ?

ScrewTurn Wiki version Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2023, Patrick Jasinski.