0

search so that I can extract the mac address of my device from a list of strings. I need to match this device name:

Device 5: Broadcom Bluetooth Device (APPLE - 00:02:72:C7:EB:AC)

and I want to extract the bdaddress in the end, that is, (00:02:72:C7:EB:AC).

How can I do that?

1 Answer 1

1
>>> import re
>>> pattern = re.compile(r'\(.* - (.*)\)')

This looks for anything in the format (<characters><space><hyphen><space><string>). Since we are interested in the we enclose that in parentheses to mark it as a group.

>>> string = 'Device 5: Broadcom Bluetooth Device (APPLE - 00:02:72:C7:EB:AC)'
>>> matches = re.search(pattern, string)

When you do a re.search on it it results in two groups:

  • group 0 - (APPLE - 00:02:72:C7:EB:AC)
  • group 1 - 00:02:72:C7:EB:AC

We are interested in group 1, so we access it as:

>>> matches.group(1)
'00:02:72:C7:EB:AC'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.