|
1 | 1 | {
|
2 | 2 | "metadata": {
|
3 | 3 | "name": "",
|
4 |
| - "signature": "sha256:295b519fef2aba4116d805cb3c00fc582bdc1a0b7da29b1ae36415f606592010" |
| 4 | + "signature": "sha256:68319934ddacbbabceac87b1876ae59e0d4fc3509192906a9483958d08c8d839" |
5 | 5 | },
|
6 | 6 | "nbformat": 3,
|
7 | 7 | "nbformat_minor": 0,
|
|
41 | 41 | "output_type": "stream",
|
42 | 42 | "stream": "stdout",
|
43 | 43 | "text": [
|
44 |
| - "Last updated: 06/07/2014 12:24:58 EDT\n", |
| 44 | + "Last updated: 06/07/2014 22:50:23 EDT\n", |
45 | 45 | "\n",
|
46 | 46 | "CPython 3.4.1\n",
|
47 | 47 | "IPython 2.1.0\n"
|
|
104 | 104 | "- [Checking for numbers](#Checking-for-numbers)\n",
|
105 | 105 | "- [Validating dates](#Validating-dates)\n",
|
106 | 106 | "- [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)" |
108 | 109 | ]
|
109 | 110 | },
|
110 | 111 | {
|
|
871 | 872 | "source": [
|
872 | 873 | "<font size=\"1px\">source: [http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx/](http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx/)</font>"
|
873 | 874 | ]
|
| 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 | + "" |
| 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 | + "" |
| 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 | + "" |
| 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 |
874 | 1064 | }
|
875 | 1065 | ],
|
876 | 1066 | "metadata": {}
|
|
0 commit comments