Created
June 15, 2016 22:32
-
-
Save nicktimko/ec38e7ad8046a7b72328a68eb69cf82d to your computer and use it in GitHub Desktop.
Lazy XML parsing with regex :(
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import re" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"xml_stuffs = '''\n", | |
"<doc>\n", | |
"<whatever></whatever>\n", | |
"<wp:postmeta>\n", | |
"<wp:meta_key><![CDATA[_ecordia_seo_info]]></wp:meta_key>\n", | |
"<wp:meta_value>(BASE_64_JUNK)</wp:meta_value>\n", | |
"</wp:postmeta>\n", | |
"<asdf>\n", | |
" <zxcv>\n", | |
" <wp:postmeta><wp:meta_key><![CDATA[_ecordia_seo_info]]></wp:meta_key><wp:meta_value>(BASE_64_JUNK)</wp:meta_value></wp:postmeta>\n", | |
" </zxcv>\n", | |
" <wp:postmeta><wp:meta_key><![CDATA[_ecordia_seo_info]]></wp:meta_key><wp:meta_value>(BASE_64_JUNK)</wp:meta_value></wp:postmeta>\n", | |
"</asdf>\n", | |
"<wp:postmeta><wp:meta_key><![CDATA[_ecordia_seo_info]]></wp:meta_key>\n", | |
"<wp:meta_value>(BASE_64_JUNK)</wp:meta_value></wp:postmeta>\n", | |
"<schmoo>\n", | |
" <wp:postmeta>\n", | |
" <wp:meta_key><![CDATA[_ecordia_seo_info]]></wp:meta_key>\n", | |
" <wp:meta_value>(BASE_64_JUNK)</wp:meta_value>\n", | |
" </wp:postmeta>\n", | |
"</schmoo>\n", | |
"</doc>\n", | |
"'''" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
"<doc>\n", | |
"<whatever></whatever>\n", | |
"\n", | |
"<asdf>\n", | |
" <zxcv>\n", | |
" \n", | |
" </zxcv>\n", | |
" \n", | |
"</asdf>\n", | |
"\n", | |
"<schmoo>\n", | |
" \n", | |
"</schmoo>\n", | |
"</doc>\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"print(re.sub('''\n", | |
" <wp:postmeta> # literal start\n", | |
" .*? # match everything (.) from 0-inf times (*) non-greedily (?)\n", | |
" </wp:postmeta> # literal end\n", | |
" ''',\n", | |
" '', # replace it with this (empty string)\n", | |
" xml_stuffs, flags=re.MULTILINE | re.VERBOSE | re.DOTALL))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment