Regular Expression Samples - .Net Framework

Examples


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
 ^([2-9]\d{2}\-|\([2-9]\d{2}\))\d{3}\-\d{4}
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 ?