0

I am writing a program to read in data from an excel sheet which contains a bunch of info about computers which will be entered by the user. This excel file will then be converted to a different format for other uses. I can read all the values no problem but I just need help validating MAC and IP addresses entered by the user to make sure the information is correct. Some regular expressions for this would be great as I am sure I would use them again at some stage.

Thanks.

Alan Smith
  • 1,069
  • 4
  • 19
  • 23
  • 3
    I'm quite sure similar questions have already been asked here. Try to search for them. – Andrew Logvinov Dec 12 '12 at 11:13
  • Have you any experience of regex? What have you tried? – garyh Dec 12 '12 at 11:14
  • A simple Google gives me [this][1] for IP addresses, and [this][2] for MAC addresses. [1]: http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address [2]: http://stackoverflow.com/questions/4260467/what-is-a-regular-expression-for-a-mac-address – Brian Agnew Dec 12 '12 at 11:14
  • Sorry I was looking for java with regex, thanks for the links. I found Java has a pattern class that can deal with regex. – Alan Smith Dec 12 '12 at 11:32

2 Answers2

1
 "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
    "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
    "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
    "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"
Giordano Maestro
  • 341
  • 1
  • 5
  • 9
1

([0-9A-F]{2}[:-]){5}([0-9A-F]{2})

This is for mac address

muthukumar
  • 2,233
  • 3
  • 23
  • 30
  • This regex will match "12-34-56:78-90:AB", which is not quite a valid MAC Address. `[0-9A-F]{2}([:-])(?:[0-9A-F]{2}\1){4}[0-9A-F]{2}` will ensure that all of the separator characters are the same, but it still misses the technically-valid "1234.5678.ABCD" format. – Andrew Rueckert Jun 30 '15 at 19:04