August 16, 2008

Regular Expressions

For matching purposes:
? = 0 or 1 match
* = 0..n matches
+ = 1..n matches
{m,n} = from 'm' to 'n' matches where 'm' and 'n' are digits
{n} = exactly 'n' matches where 'n' is a digit
\d = numeric digit
\D = anything but a numeric digit
\s = any whitespace character
\S = any non-whitespace character
\w = Any alphabetical character and '_'
\W = Any non-alphabetical character or '_'

This example matches a United Kingdom postcode:
using System.Text.RegularExpressions;
...
const string PostCodeRegEx = @"(GIR0AA|[A-PR-UWYZ]([0-9]{1,2}|" + 
  "([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|" +
  "[0-9][A-HJKS-UW])[0-9][ABD-HJLNP-UW-Z]{2})";
public bool IsPostCode(string postCode)
{
    postCode = postCode.Trim().Replace(" ", string.Empty);
    bool res = Regex.IsMatch(postCode, PostCodeRegEx);
    return res;
}
This example Matches a SID:
using System.Text.RegularExpressions;
...
result = string.Empty;
const string SID_PATTERN = @"(\w-\d-\d-\d{2}-\d+-\d+-\d+)";
Regex reg = new Regex(SID_PATTERN);
MatchCollection mc = reg.Matches(sid);
if (mc.Count > 0)
{
    result = mc[0].Value;
}

No comments: