Use regular expressions to determine if a string is a MAC address

Source: Internet
Author: User

Today boss gives a small task, asking for a string to determine whether the string is a MAC address and implemented with Java and regular expressions. So I by the way Baidu the next MAC address, and CMD under the use of Getmac to get the MAC address of the machine, understand a bit, it is implemented with Java. To facilitate testing and input and output, select Use Android as UI interaction. Anyway, with Java, for this problem, a Java project and Android are not very different.

First look at a MAC address: 48-5d-60-61-3d-c5. It consists of 6 bytes (hexadecimal), simple to understand the number of characters, uppercase and lowercase letters (a-fa-f), dashes-composition. Nonsense not to say, first write a regular expression of the Mac, I wrote a total of 3 regular expressions, is the result of continuous optimization.

Regular Expressions:

(1) "[a-fa-f0-9][a-fa-f0-9]-[a-fa-f0-9][a-fa-f0-9]-[a-fa-f0-9][a-fa-f0-9]-[a-fa-f0-9][a-fa-f0-9]-[a-fa-f0-9][ A-FA-F0-9]-[A-FA-F0-9][A-FA-F0-9] "

Although this expression looks long, it is the simplest of logic, the easiest to understand, and exactly right. A-f a-f 0-9 means one of them, the middle is a "or" relationship. Dash-can be matched directly.


(2) "[A-fa-f0-9]{2}-[a-fa-f0-9]{2}-[a-fa-f0-9]{2}-[a-fa-f0-9]{2}-[a-fa-f0-9]{2}-[a-fa-f0-9]{2}"

After optimization, the length is cut half short. {2} is to loop the previous part 2 times. On the basis of the first method, this is also easy to understand.


(3) "([a-fa-f0-9]{2}-) {5}[a-fa-f0-9]{2}"

Continue optimization on the basis of (2). The "[a-fa-f0-9]{2}-" is extracted as a unit. {5} and loop 5 times, because the polygon and the front 5 are duplicates, there is a lot of compressed space.


Code writing is simple and visually programmed on Android:

public class Mainactivity extends Activity {private EditText ismac;private Button startmac;private TextView Result;privat e static String judgemacaddress; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); Ismac = (EditText) Findviewbyid (r.id.id_edit); StartMAC = (Button) Findviewbyid (r.id.id_btn); result = (TextView) Findviewbyid (R.id.id_text); Startmac.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {judgemacaddress = Ismac.gettext (). ToString (); Boolean ismac = Stringismac (judgemacaddress); if (Ismac) {Result.settext ("This is MAC Address");} else {Result.settext ("This is not a MAC address");}});} Private Boolean Stringismac (String val) {String truemacaddress = "([a-fa-f0-9]{2}-) {5}[a-fa-f0-9]{2}";//This is the real MAV address , regular expression, if (Val.matches (truemacaddress)) {return true;} else {return false;}}}

(1) (2) (3) The result of the final implementation is as follows:





.


At this point, the MAC address has been judged. Perhaps the final type of regular expression and the method of optimization, please give criticism and suggestions.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Use regular expressions to determine if a string is a MAC address

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.