C# Regex Validating Mac Address -


i trying validate mac addresses. in instance there no - or : example valid mac either:

0000000000 00-00-00-00-00-00 00:00:00:00:00:00 

however keep getting false when run against below code:

using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.text.regularexpressions;  namespace parsingxml {     class program     {         static void main(string[] args)         {             console.write("give me mac address: ");              string input = console.readline();             input = input.replace(" ", "").replace(":","").replace("-","");              regex r = new regex("^([:xdigit:]){12}$");               if (r.ismatch(input))             {                 console.write("valid mac");             }             else             {                 console.write("invalid mac");             }             console.read();         }     } } 

output: invalid mac

.net regex does not have support posix character class. , if support, need enclose in [] make effective, i.e. [[:xdigit:]], otherwise, treated character class characters :, x, d, i, g, t.

you want regex instead (for mac address after have cleaned unwanted characters):

^[a-fa-f0-9]{12}$ 

note cleaning string of space, - , :, allow inputs shown below pass:

   34:   3-342-9fbc: 6:7 

demo


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

What is the difference between data design and data model(ERD) -

ios - Can NSManagedObject conform to NSCoding -