Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Regular Expression Samples - .Net Framework

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

Examples

  • 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
 ^([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 ?

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