Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Use a Regex to parse a GUID (and keep it unchanged at runtime)

0.00/5 (No votes)
15 Jun 2015 2  
A short Reference on how you can validate a registry-formatted GUID with a regular expression

Introduction

I assume that you bring a bit of background knowledge on regular expressions and the .Net framework with you.

Whatsoever, I keep this one straight forward: I assume that a valid registry formatted GUID looks like that:

{A9526723-C3BC-4A36-ADF8-9AC7CBDCEE52}

As you can see it's easy:

  • Open curly brackets
  • an 8 characters digit-letter combination
  • Hyphen
  • a 4 characters digit-letter combination, repeating 3 times (each of the repeating sequences separated by a hyphen)
  • a 12 characters digit-letter combination,
  • and closing curly brackets.

Using the code

The regex itself looks pretty straightforward:

^{[A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}}$

I built it into the CoreResource class of the Springlog Project, a Syslog implementation on .Net which is currently under heavy development:

using System;
using System.Text.RegularExpressions;

namespace Springlog.Core
{
    internal static class CoreResources
    {
        static string customerGuid = "{A9526723-C3BC-4A36-ADF8-9AC7CBDCEE52}";
        /// <summary>
        /// GUID to identify the application which uses the Springlog.Core
        /// library.
        /// </summary>
        internal static string CustomerGuid
        {
            get { return CoreResources.customerGuid; }
            set 
            {
                Regex regex = new Regex("^{[A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}}$");
                Match match = regex.Match(value);
                if (match.Success)
                {
                    CoreResources.customerGuid = value; 
                }
                else
                {
                    throw new ArgumentOutOfRangeException("The submitted string is not a valid GUID. Springlog.Core.CoreResources.CustomerGuid "+
                        "needs to be formatted as registry GUID, for example \"{A9526723-C3BC-4A36-ADF8-9AC7CBDCEE52}\"");
                }
            }
        }
    }
}

You may ask yourself why I marked this class as internal, don't you? There is a facade before it in order to be able to control the outside access to the CoreResources class, called Configurator.

namespace Springlog.Core
{
    public class Configurator
    {
        public void ConfigureCore(string customerGuid)
        {
            CoreResources.CustomerGuid = customerGuid;
        }
    }
}

I admit that this construction may be confusing, but it allows me to abstract the Resources class from what the other applications sees outside. For example, if I decide to rename CustomerGuid to CustomerAppGuid it won't affect the application which uses Springlog. Apart from that it allows me to use the Configurator class as access controller and for example prevent the main application (which uses Springlog) from changing the GUID more than once a runtime:

namespace Springlog.Core
{
    public class Configurator
    {
        bool customerGuidSet = false;
        public void ConfigureCore(string customerGuid)
        {
            if(!customerGuidSet){
               CoreResources.CustomerGuid = customerGuid;
               customerGuidSet = true;
            }
        }
    }
}

The easy way

I have to admit that I was new to Regex and found it a way to play and learn when I wrote this Article. Fellow CodeProject Member Matthew Dennis then pointed at the Guid.TryParse Method which (of course) is much easier to use and let's adapt the GUID format much better than a self tweaked Regex. Here's the sample code he gave me:

using System;
 
namespace ConsoleApplication2
{
    internal static class CoreResources
    {
        static string customerGuid = "{A9526723-C3BC-4A36-ADF8-9AC7CBDCEE52}";
        /// 
        /// GUID to identify the application which uses the Springlog.Core
        /// library.
        /// 
        internal static string CustomerGuid
        {
            get { return CoreResources.customerGuid; }
            set 
            {
                Guid temp;
                if (Guid.TryParse(value, out temp))
                {
                    CoreResources.customerGuid = value; 
                }
                else
                {
                    throw new ArgumentOutOfRangeException("CustomerGuid", "The submitted string is not a valid GUID. Springlog.Core.CoreResources.CustomerGuid "+
                        "needs to be formatted as registry GUID, for example \"{A9526723-C3BC-4A36-ADF8-9AC7CBDCEE52}\"");
                }
            }
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Initial Value = " + CoreResources.CustomerGuid);
                var newGuid = Guid.NewGuid();
 
                string newGuidString = newGuid.ToString();
                Console.WriteLine("Setting Value to " + newGuidString);
                CoreResources.CustomerGuid = newGuidString;
                Console.WriteLine("Current Value = " + CoreResources.CustomerGuid);
 
                newGuidString = "{" + newGuidString + "}";
                Console.WriteLine("Setting Value to " + newGuidString);
                CoreResources.CustomerGuid = newGuidString;
                Console.WriteLine("Current Value = " + CoreResources.CustomerGuid);
 
               newGuidString = "This is not a GUID.";
                Console.WriteLine("Setting Value to " + newGuidString);
                CoreResources.CustomerGuid = newGuidString;
                Console.WriteLine("Current Value = " + CoreResources.CustomerGuid);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception : {0}", ex.Message);
            }
 
            Console.ReadLine();
        }
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here