Regular expression to Extract mac address and wireless network id in java

Hi Experts:

I need an expression to extract mac address and wireless network id:

00-1E-4C-32-DA-2E:ssid1

Right now I use the following code (I have posted the code fragment)

    Pattern pattern = Pattern.compile("(.*):(.*)");
      Matcher matcher = pattern.matcher(Csid);
      if(matcher.matches())
      {
         System.out.print("Key:");
         bssid = matcher.group(1);
         System.out.println(matcher.group(1));
         System.out.print("Value:");
         System.out.println(matcher.group(2));

Csid is the String contain mac address and ssid.

But this code does not work when there is no wireless network id present, for example:

00-1E-4C-32-DA-2E

Please give me an expression to parse and extract both and the code fragment.
00-1E-4C-32-DA-2E:ssid1
or
00-1E-4C-32-DA-2E

It should extact both.

Best Regards,

Sunnybrad
sunnybradAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
CEHJConnect With a Mentor Commented:
Try the below. If groupCount == 2, only collect group 2 if it's not null
Pattern p = Pattern.compile("((?:[0-9A-F]{2}-){5}[0-9A-F]{2})(:[^ ]+)*");

Open in new window

0
 
sunnybradAuthor Commented:
Hi:

Thanks for the comment, please give some comments whats groupcount. Shall I fir this in my code.

Thanks

Sunnybrad
0
 
sunnybradAuthor Commented:
Hi:

How do you extract group one and group two the above regular expression breaks as well at run-time.

Best regards

Sunnybrad
0
 
sunnybradAuthor Commented:
Hi:

I have solved this by saying if you don't match (.*):(.*).

Then the ssid part is null.

Thanks

Sunnybrad
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.