kmaid Posted October 4, 2008 Share Posted October 4, 2008 Hello I could use some help with regex validating a string. The string is hexadecimal (uses 0-9 a-f character set) and 36 characters long broken up with dashes into 5 sections eg: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx Here are some examples of valid keys: a822ff2b-ff02-461d-b45d-dcd10a2de0c2 f63f298d-0989-49ff-8aa3-5897e259e767 8e589107-5f97-4e60-8194-11fcda76c194 c428132b-fd96-4b34-9bbe-9e627a5a50e4 2ceb9b3e-cf25-446b-b04a-dccae9d0a77a A regex expression that can validate the correct character set, format and number of characters would be very helpful and be interesting to compare against what I am playing with. Thanks Kmaid Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 4, 2008 Share Posted October 4, 2008 Try this out: <?php $tests = array( "a822ff2b-ff02-461d-b45d-dcd10a2de0c2", "f63f298d-0989-49ff-8aa3-5897e259e767", "8e589107-5f97-4e60-8194-11fcda76c194", "c428132b-fd96-4b34-9bbe-9e627a5a50e4", "2ceb9b3e-cf25-446b-b04a-dccae9d0a77a", "2ceb9b7e-cf25-421b-b04a-dccae9d0a77z", ); foreach($tests as $t){ if(preg_match("#[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}#i", $t)){ echo $t . ": <b>Valid</b><br />"; }else{ echo $t . ": <b>Invalid</b><br />"; } } ?> Quote Link to comment Share on other sites More sharing options...
kmaid Posted October 4, 2008 Author Share Posted October 4, 2008 Looks perfect and is working for all the test data i can come up with. Thank you ProjectFear! Quote Link to comment Share on other sites More sharing options...
thebadbad Posted October 4, 2008 Share Posted October 4, 2008 I think you would want to check the whole string only - i.e. add ^ and $ (and the D modifier) to the regex. Else something like "#¤%&/a822ff2b-ff02-461d-b45d-dcd10a2de0c2" will pass. if(preg_match("#^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$#iD", $t)){ Quote Link to comment Share on other sites More sharing options...
kmaid Posted October 4, 2008 Author Share Posted October 4, 2008 ThebadBad, Good spot i have adjusted accordingly :3 Thank you both Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted October 7, 2008 Share Posted October 7, 2008 When you have repeating sub-patterns (such as -[\da-f]{4}) which in this case is repeated 3 times, you can use recursive expressions. The idea is simple... after the first capture in parenthesis, you can 'reuse' this capture which effectively shortens and simplifies the code from a readability standpoint: #^[\da-f]{8}(-[\da-f]{4})(?1)(?1)-[\da-f]{12}$# Cheers, NRG Quote Link to comment Share on other sites More sharing options...
thebadbad Posted October 7, 2008 Share Posted October 7, 2008 Cool. I was pretty sure it could be done, just didn't know how. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.