Regexp question

I use this pattern to validate mac address:

$reg = "/^[a-f0-9]{,2}:[a-f0-9]{,2}:[a-f0-9]{,2}:[a-f0-9]{,2}:[a-f0-9]{,2}:[a-f0-9]{,2}/";

each group must be 2 characters, not less not more, I tried all {2} {,2} {2,} {2,2} and each of them had a problem to validate it with just 2 characters, another condition is that these 2-digits must be 6 groups (separated by: ), not less, not more. please advice how this regexp should be?

you are aware that upper-case letters do not validate?

I did change to uppercase. I know lowercase validates too, but to have a harmony in log I do uppercase all of them as this is not nice to have both lower/upper case in log however both are valid.
PS. I got what you meant! Actually before it reaches to preg_match I do use strtoupper().

then you should remove the repetition.

# using \w since it’s less to type 
/^\w{2}(:\w{2}){5}$/

So that it doesn’t accept invalid characters such as underscores or letters after F but does accept both lower and uppercase you can use:

/^[a-f0-9]{2}(:[a-f0-9]{2}){5}$/i

Thanks,

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.