Regular Expression

  • Thread starter Thread starter Pratik Parikh
  • Start date Start date
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")
}
 
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
 
Back
Top