8000 check for IP and MAC addresses · ilovejs/python_reference@c24f1c6 · GitHub
[go: up one dir, main page]

Skip to content

Commit c24f1c6

Browse files
committed
check for IP and MAC addresses
1 parent 5985186 commit c24f1c6

File tree

4 files changed

+193
-3
lines changed

4 files changed

+193
-3
lines changed

Images/Ipv4_address.png

22.9 KB
Loading

Images/Ipv6_address.png

52 KB
Loading

Images/MACaddressV3.png

7.93 KB
Loading

tutorials/useful_regex.ipynb

Lines changed: 193 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:295b519fef2aba4116d805cb3c00fc582bdc1a0b7da29b1ae36415f606592010"
4+
"signature": "sha256:68319934ddacbbabceac87b1876ae59e0d4fc3509192906a9483958d08c8d839"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -41,7 +41,7 @@
4141
"output_type": "stream",
4242
"stream": "stdout",
4343
"text": [
44-
"Last updated: 06/07/2014 12:24:58 EDT\n",
44+
"Last updated: 06/07/2014 22:50:23 EDT\n",
4545
"\n",
4646
"CPython 3.4.1\n",
4747
"IPython 2.1.0\n"
@@ -104,7 +104,8 @@
104104
"- [Checking for numbers](#Checking-for-numbers)\n",
105105
"- [Validating dates](#Validating-dates)\n",
106106
"- [Time](#Time)\n",
107-
"- [Checking for HTML tags](#Checking-for-HTML-tags)"
107+
"- [Checking for HTML tags](#Checking-for-HTML-tags)\n",
108+
"- [Checking for IP addresses](#Checking-for-IP-addresses)"
108109
]
109110
},
110111
{
@@ -871,6 +872,195 @@
871872
"source": [
872873
"<font size=\"1px\">source: [http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx/](http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx/)</font>"
873874
]
875+
},
876+
{
877+
"cell_type": "markdown",
878+
"metadata": {},
879+
"source": [
880+
"<br>\n",
881+
"<br>"
882+
]
883+
},
884+
{
885+
"cell_type": "heading",
886+
"level": 2,
887+
"metadata": {},
888+
"source": [
889+
"Checking for IP addresses"
890+
]
891+
},
892+
{
893+
"cell_type": "markdown",
894+
"metadata": {},
895+
"source": [
896+
"[[back to top](#Sections)]"
897+
]
898+
},
899+
{
900+
"cell_type": "heading",
901+
"level": 3,
902+
"metadata": {},
903+
"source": [
904+
"IPv4"
905+
]
906+
},
907+
{
908+
"cell_type": "markdown",
909+
"metadata": {},
910+
"source": [
911+
"![](../Images/Ipv4_address.png)"
912+
]
913+
},
914+
{
915+
"cell_type": "markdown",
916+
"metadata": {},
917+
"source": [
918+
"<font size=\"1px\">Image source: http://en.wikipedia.org/wiki/File:Ipv4_address.svg</font>"
919+
]
920+
},
921+
{
922+
"cell_type": "code",
923+
"collapsed": false,
924+
"input": [
925+
"pattern = r'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'\n",
926+
"\n",
927+
"str_true = ('172.16.254.1', '1.2.3.4', '01.102.103.104', )\n",
928+
" \n",
929+
"str_false = ('17216.254.1', '1.2.3.4.5', '01 .102.103.104', )\n",
930+
"\n",
931+
"for t in str_true:\n",
932+
" assert(bool(re.match(pattern, t)) == True), '%s is not True' %t\n",
933+
"\n",
934+
"for f in str_false:\n",
935+
" assert(bool(re.match(pattern, f)) == False), '%s is not False' %f"
936+
],
937+
"language": "python",
938+
"metadata": {},
939+
"outputs": [],
940+
"prompt_number": 8
941+
},
942+
{
943+
"cell_type": "markdown",
944+
"metadata": {},
945+
"source": [
946+
"<font size=\"1px\">source: [http://answers.oreilly.com/topic/318-how-to-match-ipv4-addresses-with-regular-expressions/](http://answers.oreilly.com/topic/318-how-to-match-ipv4-addresses-with-regular-expressions/)</font>"
947+
]
948+
},
949+
{
950+
"cell_type": "heading",
951+
"level": 3,
952+
"metadata": {},
953+
"source": [
954+
"Ipv6"
955+
]
956+
},
957+
{
958+
"cell_type": "markdown",
959+
"metadata": {},
960+
"source": [
961+
"![](../Images/Ipv6_address.png)"
962+
]
963+
},
964+
{
965+
"cell_type": "markdown",
966+
"metadata": {},
967+
"source": [
968+
"<font size=\"1px\">Image source: http://upload.wikimedia.org/wikipedia/commons/1/15/Ipv6_address.svg</font>"
969+
]
970+
},
971+
{
972+
"cell_type": "code",
973+
"collapsed": false,
974+
"input": [
975+
"pattern = r'^\\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?\\s*$'\n",
976+
"\n",
977+
"str_true = ('2001:470:9b36:1::2',\n",
978+
" '2001:cdba:0000:0000:0000:0000:3257:9652', \n",
979+
" '2001:cdba:0:0:0:0:3257:9652', \n",
980+
" '2001:cdba::3257:9652', )\n",
981+
" \n",
982+
"str_false = ('1200::AB00:1234::2552:7777:1313', # uses `::` twice\n",
983+
" '1200:0000:AB00:1234:O000:2552:7777:1313', ) # contains an O instead of 0\n",
984+
"\n",
985+
"for t in str_true:\n",
986+
" assert(bool(re.match(pattern, t)) == True), '%s is not True' %t\n",
987+
"\n",
988+
"for f in str_false:\n",
989+
" assert(bool(re.match(pattern, f)) == False), '%s is not False' %f"
990+
],
991+
"language": "python",
992+
"metadata": {},
993+
"outputs": [],
994+
"prompt_number": 21
995+
},
996+
{
997+
"cell_type": "markdown",
998+
"metadata": {},
999+
"source": [
1000+
"<font size=\"1px\">source: [http://snipplr.com/view/43003/regex--match-ipv6-address/](http://snipplr.com/view/43003/regex--match-ipv6-address/)</font>"
1001+
]
1002+
},
1003+
{
1004+
"cell_type": "markdown",
1005+
"metadata": {},
1006+
"source": [
1007+
"<br>\n",
1008+
"<br>"
1009+
]
1010+
},
1011+
{
1012+
"cell_type": "heading",
1013+
"level": 2,
1014+
"metadata": {},
1015+
"source": [
1016+
"Checking for MAC addresses"
1017+
]
1018+
},
1019+
{
1020+
"cell_type": "markdown",
1021+
"metadata": {},
1022+
"source": [
1023+
"[[back to top](#Sections)]"
1024+
]
1025+
},
1026+
{
1027+
"cell_type": "markdown",
1028+
"metadata": {},
1029+
"source": [
1030+
"![](../Images/MACaddressV3.png)"
1031+
]
1032+
},
1033+
{
1034+
"cell_type": "markdown",
1035+
"metadata": {},
1036+
"source": [
1037+
"<font size=\"1px\">Image source: http://upload.wikimedia.org/wikipedia/en/3/37/MACaddressV3.png </font>"
1038+
]
1039+
},
1040+
{
1041+
"cell_type": "code",
1042+
"collapsed": false,
1043+
"input": [
1044+
"pattern = r'^(?i)([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$'\n",
1045+
"\n",
1046+
"str_true = ('94-AE-70-A0-66-83', \n",
1047+
" '58-f8-1a-00-44-c8',\n",
1048+
" '00:A0:C9:14:C8:29'\n",
1049+
" , )\n",
1050+
" \n",
1051+
"str_false = ('0:00:00:00:00:00', \n",
1052+
" '94-AE-70-A0 -66-83', ) \n",
1053+
"\n",
1054+
"for t in str_true:\n",
1055+
" assert(bool(re.match(pattern, t)) == True), '%s is not True' %t\n",
1056+
"\n",
1057+
"for f in str_false:\n",
1058+
" assert(bool(re.match(pattern, f)) == False), '%s is not False' %f"
1059+
],
1060+
"language": "python",
1061+
"metadata": {},
1062+
"outputs": [],
1063+
"prompt_number": 29
8741064
}
8751065
],
8761066
"metadata": {}

0 commit comments

Comments
 (0)
0