Mar 11, 2007

Notes on C# Regular Expression library.

Example Code

Match m = Regex.Match("www.yahoo.com", "[\\W]+yahoo[\\W]+");
if (m.Success)
{
    Console.WriteLine("matched "+ m.ToString());
}
else
{
    Console.WriteLine("no match");
}
m = Regex.Match("www.yahoo.com", "[\\W]+yahoo[\\W]+", RegexOptions.IgnoreCase);


Regex regex = new Regex("^https?://[^/]+(.*)");
string sReferrer = "http://www.yahoo.com/testing/onetwothree/yikes#1234?asp3";
Console.WriteLine("sReferrer=" + sReferrer);
Match match=regex.Match(sReferrer);
if (match.Success) {
    int[] regexGroup = regex.GetGroupNumbers();
Console.WriteLine("regex # match:"+match.Groups[1].ToString());
}
else {
    Console.WriteLine("regex match failed");
}

Example from MSDN

Regex rx = new Regex(@"^-?\d+(\.\d{2})?$");
    
    // Define some test strings.
    string[] tests = {"-42", "19.99", "0.001", "100 USD"};
    
    // Check each test string against the regular expression.
    foreach (string test in tests)
    {
    if (rx.IsMatch(test))
        {
            Console.WriteLine("{0} is a currency value.", test);
        }
    }

Email Example

	System.Net.Mail.MailMessage _mail = null;
	_mail = new System.Net.Mail.MailMessage("dkim@example.com", 
		"dest@example.com", "subject", "body");

	System.Net.Mail.SmtpClient _smtp = new System.Net.Mail.SmtpClient("mail.legalzoom.com");
	_smtp.Credentials = new System.Net.NetworkCredential(@"dkim@legalzoom.com", 
		"MYPASSWORD");
	_smtp.Send(_mail);
	_mail = null;

    System.Text.RegularExpressions.RegEx

Match(text): Finds first match & uses Match.Success to determine if match was found. Uses \\ instead of \ for escaped char. Example

			Regex r;
	        Match m;
	        r = new Regex("href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))",
	            RegexOptions.IgnoreCase|RegexOptions.Compiled);
	        for (m = r.Match(inputString); m.Success; m = m.NextMatch()) 
	        {
	            Console.WriteLine("Found href " + m.Groups[1] + " at " 
	                + m.Groups[1].Index);
	        }

Matches(text, pattern, options) : Static function, returns MatchCollection.

	using System.Text.RegularExpressions
	string text = @"........";
	string pattern = "ion/b";
	MatchCollection matches= Regex.Matches(text, pattern,
		RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
		foreach (Match i in matches){
				Debug.WriteLine(i.ToString());
			}

MatchCollection.ToString() : gets the actual matched string. MatchCollection.Index() : gets the index # where match occured.

Regular Expression Pattern String

	^: beginning of text
	$: end of text
	.: any single char except \n.   b.y=bay,boy,...
	*: 0 or more char.  ca*t = cat, cast, carat, ...
	+: 1 or more char.  ca+t = cast, carat, but not "cat"
	?: 0 or 1 char.  ca?t = cat, cart, ... but not carat
	\s: any white space
	\S: any non-white space char
	\b: word boundary.   cat\b = any word ending in cat
	\B: any position tht isn't word boundary. \Bx\B = any x in middle of word
	\w: word char & underscore[a-zA-Z_0-9] 
	\W: non-word char [^a-zA-Z_0-9]
	\D: non-digit [^0-9]
	\d: digit [0-9]

Split(text,pattern,option) returns array of strings split using regex

Example

		Regex r = new Regex("(-)"); // Split on hyphens. 
		string[] s = r.Split("one-two-banana");
		output:  [ "one", "-", "two", "-", "banana" ]

GetGroupNumbers() Returns an array of capturing group numbers that correspond to group names in an array.