regex guid tester

  • Thread starter Thread starter Greg Merideth
  • Start date Start date
G

Greg Merideth

I came up with this to test strings passed into a checking method to see
if the guid being passed in (regular 128 bit windows registry GUID) is
valid.

So far it seems to handle most errors thrown at it but today during a
test, a bracket "}" passed through the method as a valid guid so I
wanted to know if this expresion will stop invalid guids or did I miss
something?

__systemGUID is the string passed in.

if(!Regex.IsMatch(__systemGUID,
@"^([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})"))

Thanks.
 
Greg Merideth said:
I came up with this to test strings passed into a checking method to see if the guid being passed in (regular 128 bit windows
registry GUID) is valid.

So far it seems to handle most errors thrown at it but today during a test, a bracket "}" passed through the method as a valid
guid so I wanted to know if this expresion will stop invalid guids or did I miss something?

__systemGUID is the string passed in.

if(!Regex.IsMatch(__systemGUID, @"^([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})"))

The non-dash stuff should be hexadecimal, not any old
alphanumeric character.
 
I came up with this to test strings passed into a checking
method to see if the guid being passed in (regular 128 bit
windows registry GUID) is valid.

So far it seems to handle most errors thrown at it but today
during a test, a bracket "}" passed through the method as a
valid guid so I wanted to know if this expresion will stop
invalid guids or did I miss something?

__systemGUID is the string passed in.

if(!Regex.IsMatch(__systemGUID,
@"^([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})"))

Thanks.

Greg,

To expand on Larry's answer, \w matches way too many different
characters. Your regex matches strings like this:

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Instead of \w, a character class of [\dA-Fa-f] should be used:

^([\dA-Fa-f]{8}-[\dA-Fa-f]{4}-[\dA-Fa-f]{4}-[\dA-Fa-f]{4}-[\dA-Fa-f]{12})
 
The following also takes into account curly braces in a guid, and it
contains a end of line marker ($).

^\{?[\dA-Fa-f]{8}-[\dA-Fa-f]{4}-[\dA-Fa-f]{4}-[\dA-Fa-f]{4}-[\dA-Fa-f]{12}\}?$

Without the end-of line, the following would slip through:
CEEBB5B7-5AB7-4605-B5B3-39D8A9511517NOT_VALID!!!!

Regards,
Joakim
I came up with this to test strings passed into a checking
method to see if the guid being passed in (regular 128 bit
windows registry GUID) is valid.

So far it seems to handle most errors thrown at it but today
during a test, a bracket "}" passed through the method as a
valid guid so I wanted to know if this expresion will stop
invalid guids or did I miss something?

__systemGUID is the string passed in.

if(!Regex.IsMatch(__systemGUID,
@"^([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})"))

Thanks.


Greg,

To expand on Larry's answer, \w matches way too many different
characters. Your regex matches strings like this:

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Instead of \w, a character class of [\dA-Fa-f] should be used:

^([\dA-Fa-f]{8}-[\dA-Fa-f]{4}-[\dA-Fa-f]{4}-[\dA-Fa-f]{4}-[\dA-Fa-f]{12})
 
Ahh that damm "$" is what I forgot to add to the regex check. Must be
old age.

Thanks.

Joakim said:
The following also takes into account curly braces in a guid, and it
contains a end of line marker ($).

^\{?[\dA-Fa-f]{8}-[\dA-Fa-f]{4}-[\dA-Fa-f]{4}-[\dA-Fa-f]{4}-[\dA-Fa-f]{12}\}?$


Without the end-of line, the following would slip through:
CEEBB5B7-5AB7-4605-B5B3-39D8A9511517NOT_VALID!!!!

Regards,
Joakim
I came up with this to test strings passed into a checking
method to see if the guid being passed in (regular 128 bit
windows registry GUID) is valid.

So far it seems to handle most errors thrown at it but today
during a test, a bracket "}" passed through the method as a
valid guid so I wanted to know if this expresion will stop
invalid guids or did I miss something?

__systemGUID is the string passed in.

if(!Regex.IsMatch(__systemGUID,
@"^([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})"))

Thanks.



Greg,

To expand on Larry's answer, \w matches way too many different
characters. Your regex matches strings like this:

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Instead of \w, a character class of [\dA-Fa-f] should be used:

^([\dA-Fa-f]{8}-[\dA-Fa-f]{4}-[\dA-Fa-f]{4}-[\dA-Fa-f]{4}-[\dA-Fa-f]{12})
 
Back
Top