I’ve been searching for a good regular expression to validate a string representation of a GUID, and even though there are 17 results on RegExLib.com and 73000+ on Google, I’ve not found one that matches the start and end braces correctly. Every single one I’ve found (without exaggerating, I read over 60 posts while looking)either matches a GUID without the braces, or those that match on a string including the braces allow for either only the first or last to be present.
^(?<-BRACE>\{)?[a-fA-F\d]{8}-(?:[a-fA-F\d]{4}-){3}[a-fA-F\d]{12}(?<-BRACE>\})?(?(BRACE)^.)$
On a side note: The project I’m doing maintenance on is VS2008/.Net 3.5. If you’re using .Net Framework 4, you can use the Guid.Parse and Guid.TryParse methods to validate a Guid.
UPDATE: I’ve submitted it to RegExLib.com.