Showing posts with label regular expression. Show all posts
Showing posts with label regular expression. Show all posts

Wednesday, 2 July 2008

How do I validate a string Guid in C#.NET?

using System;
using System.Text.RegularExpressions;

public static class GuidUtility
{
public static bool IsGuid( string guid )
{
if ( !string.IsNullOrEmpty( guid ) )
{
Regex regex = new Regex( @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$" );

return regex.IsMatch( guid );
}

return false;
}
}

Thursday, 19 June 2008

How do I remove the port number from a URL in C#.NET?

Regular Expression:
:{1}\d*

Using C#.NET, the above regular expression can be applied to a URL to strip the port number (along with ":"):

Regex regex = new Regex( @":{1}\d{1,}" );
string url = regex.Replace( "http://andrewgunn.blogspot.com:81/", string.Empty );