{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Regular Expressions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic Matching" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "None\n", "\n", "\n", "\n", "\n", "\n", "\n", "None\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "['dog', 'cat']\n" ] } ], "source": [ "import re\n", "result1 = re.search(r\"aza\", \"plaza\")\n", "result2 = re.search(r\"aza\", \"bazaar\")\n", "print(result1)\n", "print(result2)\n", "print(re.search(r\"aza\", \"maze\"))\n", "print(re.search(r\"^x\", \"xenon\"))\n", "print(re.search(r\"p.ng\", \"clapping\"))\n", "print(re.search(r\"p.ng\", \"sponge\"))\n", "print(re.search(r\"p.ng\", \"Pangaea\", re.IGNORECASE))\n", "print(re.search(r\"[Pp]ython\", \"Python\"))\n", "print(re.search(r\"[a-z]way\", \"The end of the highway\"))\n", "print(re.search(r\"[a-z]way\", \"What a way to go\"))\n", "print(re.search(\"cloud[a-zA-Z0-9]\", \"cloudy\"))\n", "print(re.search(\"cloud[a-zA-Z0-9]\", \"cloud9\"))\n", "print(re.search(r\"[^a-zA-Z]\", \"This is a sentence with spaces.\"))\n", "print(re.search(r\"[^a-zA-Z ]\", \"This is a sentence with spaces.\"))\n", "print(re.search(r\"cat|dog\", \"I like cats.\"))\n", "print(re.search(r\"cat|dog\", \"I love dogs!\"))\n", "print(re.search(r\"cat|dog\", \"I like both dogs and cats.\"))\n", "print(re.search(r\"cat|dog\", \"I like cats.\"))\n", "print(re.search(r\"cat|dog\", \"I love dogs!\"))\n", "print(re.search(r\"cat|dog\", \"I like both dogs and cats.\"))\n", "print(re.findall(r\"cat|dog\", \"I like both dogs and cats.\"))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "False\n", "True\n", "True\n", "False\n", "True\n", "True\n", "False\n" ] } ], "source": [ "import re\n", "def check_aei (text):\n", " result = re.search(r\"a.e.i\", text)\n", " return result != None\n", "\n", "print(check_aei(\"academia\")) # True\n", "print(check_aei(\"aerial\")) # False\n", "print(check_aei(\"paramedic\")) # True\n", "\n", "def check_punctuation (text):\n", " result = re.search(r\"[,.:;?!]\", text)\n", " return result != None\n", "\n", "print(check_punctuation(\"This is a sentence that ends with a period.\")) # True\n", "print(check_punctuation(\"This is a sentence fragment without a period\")) # False\n", "print(check_punctuation(\"Aren't regular expressions awesome?\")) # True\n", "print(check_punctuation(\"Wow! We're really picking up some steam now!\")) # True\n", "print(check_punctuation(\"End of the line\")) # False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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.12.3" } }, "nbformat": 4, "nbformat_minor": 2 }