Skip to content

Instantly share code, notes, and snippets.

@guyoun
Created June 13, 2016 03:16
Show Gist options
  • Save guyoun/c7a680175a992bc046c809b8cee21f22 to your computer and use it in GitHub Desktop.
Save guyoun/c7a680175a992bc046c809b8cee21f22 to your computer and use it in GitHub Desktop.
re module example
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(test/a/)|(test/b/)\n",
"Some regex matched!\n"
]
}
],
"source": [
"import re\n",
"\n",
"regexes = [\n",
" \"test/a/\",\n",
" \"test/b/\",\n",
" ]\n",
"\n",
"# Make a regex that matches if any of our regexes match.\n",
"combined = \"(\" + \")|(\".join(regexes) + \")\"\n",
"print combined \n",
"\n",
"mystring = \"test/b/\"\n",
"\n",
"if re.match(combined, mystring):\n",
" print \"Some regex matched!\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment