Regular Expression

P

Pratik Parikh

Hi Everyone,

I need a way to validate GUID using regular expression.

my code:
Code:
string test = "{21B8D827-2F5B-4E49-B25B-DBA29EBD5A5E}"
Regex r = new Regex("(\\{*\\})");
if(r.Matches(test)){
Response.Write("It works")
}
 
R

Ricky K. Rasmussen

It should be something like this:

Code:
string test = "{21B8D827-2H5B-4E49-B25B-DBA29EBD5A5E}";
Regex re = new
Regex("\\{[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}\\}");
if(re.Match(test).Success){
MessageBox.Show("It works");
}

Or maybe you could use the class System.Guid, and then catch the exception
if that fails. Depends on how your code is organized:

Code:
string test = "{21B8D827-2H5B-4E49-B25B-DBA29EBD5A5E}";
try
{
System.Guid id = new System.Guid(test);
MessageBox.Show("It works");
}
catch
{
//Do some exceptionhandling like alerting the user
}

/ricky
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top