Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added mac addresses #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CommonRegex
===========

Find all times, dates, links, phone numbers, emails, ip addresses, prices, hex colors, and credit card numbers in a string.
Find all times, dates, links, phone numbers, emails, ip addresses, mac addresses, prices, hex colors, and credit card numbers in a string.
We did the hard work so you don't have to.

Pull requests welcome!
Expand Down Expand Up @@ -80,6 +80,7 @@ Supported Methods/Attributes
- `obj.emails`, `obj.emails()`
- `obj.ips`, `obj.ips()`
- `obj.ipv6s`, `obj.ipv6s()`
- `obj.mac_addresses`, `obj.mac_addresses()`
- `obj.prices`, `obj.prices()`
- `obj.hex_colors`, `obj.hex_colors()`
- `obj.credit_cards`, `obj.credit_cards()`
Expand Down
2 changes: 2 additions & 0 deletions commonregex.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
email = re.compile("([a-z0-9!#$%&'*+\/=?^_`{|.}~-]+@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)", re.IGNORECASE)
ip = re.compile('(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)', re.IGNORECASE)
ipv6 = re.compile('\s*(?!.*::.*::)(?:(?!:)|:(?=:))(?:[0-9a-f]{0,4}(?:(?<=::)|(?<!::):)){6}(?:[0-9a-f]{0,4}(?:(?<=::)|(?<!::):)[0-9a-f]{0,4}(?:(?<=::)|(?<!:)|(?<=:)(?<!::):)|(?:25[0-4]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-4]|2[0-4]\d|1\d\d|[1-9]?\d)){3})\s*', re.VERBOSE|re.IGNORECASE|re.DOTALL)
mac_address = re.compile(r'(?:[a-fA-F0-9]{2}-){5}[a-fA-F0-9]{2}|(?:[a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}|(?:[0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4}')
price = re.compile('[$]\s?[+-]?[0-9]{1,3}(?:(?:,?[0-9]{3}))*(?:\.[0-9]{1,2})?')
hex_color = re.compile('(#(?:[0-9a-fA-F]{8})|#(?:[0-9a-fA-F]{3}){1,2})\\b')
credit_card = re.compile('((?:(?:\\d{4}[- ]?){3}\\d{4}|\\d{15,16}))(?![\\d])')
Expand All @@ -28,6 +29,7 @@
"emails" : email,
"ips" : ip,
"ipv6s" : ipv6,
"mac_addresses" : mac_address,
"prices" : price,
"hex_colors" : hex_color,
"credit_cards" : credit_card,
Expand Down
13 changes: 13 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ def test_ipv6s(self):
self.assertEqual(self.parser.ipv6s(s), [s])


class TestMacAddresses(RegexTestCase):

def test_mac_addresses(self):
matching = ["01-23-45-67-89-AB", "FE:23:45:67:89:ab", "0123.4567.89aB"]
non_matching = ["01-23-45-67-89-FG", "GF:23:45:67:89:AB", "G123.4567.89AB",
"101-23-45-67-89-AA", "1FF:23:45:67:89:AB", "F123.4567.89ABC",
"1123-45-67-89-AA", "11:23::45:67:89:AB", "F123..4567.1111"]
for s in matching:
self.assertEqual(self.parser.mac_addresses(s), [s])
for s in non_matching:
self.assertNotEqual(self.parser.mac_addresses(s), [s])


class TestPrices(RegexTestCase):

def test_prices(self):
Expand Down