tags:

views:

384

answers:

4

Trying to figure out the Regex pattern to match if an email contains a Guid, e.g.

[email protected]

The Guid could potentially be anywhere before the @, e.g.

[email protected]

+3  A: 

I use this to find Guids

Regex isGuid = 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}$", RegexOptions.Compiled);
WOPR
A: 

Well, assuming it's always going to be in standard GUID notation like that, if the following regex matches there was a GUID. You should also apply your language's method of making it case-insensitive.

[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}[^@]*@
Chad Birch
A: 
^[^@]*([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})

will match the any hex in the 8-4-4-4-12 format that comes before a @

cobbal
A: 

A lazy variant would be

([0-9a-f-]{36}).*?@

It is easy to read and I bet it matches 99,99% of all cases ;) But then in 0,00001% of all cases sombody could have an email address that fits in a GUID scheme.

Norbert Hartl