diff --git a/Sanjay/Regular_Expression.ipynb b/Sanjay/Regular_Expression.ipynb new file mode 100644 index 0000000..6b06a45 --- /dev/null +++ b/Sanjay/Regular_Expression.ipynb @@ -0,0 +1,418 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "id": "xMLU30SQxZCH" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "cat\n" + ] + } + ], + "source": [ + "#Write the regular expression and use proper method which gives output as:- 'cat' in str.\n", + "import re\n", + "str = 'cat mat bat rat'\n", + "output=re.match(r\"c\\w\\w\",str)\n", + "if output : \n", + " print(output.group())\n", + "else : \n", + " print(\"No such word found\")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "id": "zMiSTGMHx6Dv" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "rat\n" + ] + } + ], + "source": [ + "#Write the regular expression and use proper method which gives output as:- 'rat' in str.\n", + "import re\n", + "str = 'cat mat bat rat'\n", + "output=re.search(r\"r\\w\\w\",str)\n", + "if output : \n", + " print(output.group())\n", + "else : \n", + " print(\"No such word found\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "id": "qBoYFjf5x6Mm" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['mat', 'man']\n" + ] + } + ], + "source": [ + "#Write the regular expression and use proper method which gives output as:- ['mat', 'man'] in str.\n", + "import re\n", + "str = 'cat mat bat rat man'\n", + "output=re.findall(r\"m\\w\\w\",str)\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "id": "qmxmqzX_x6QR" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Python', 's', 'Programming', 'is', 'very', 'easy', 'to', 'learn']\n" + ] + } + ], + "source": [ + "#Write the regular expression and use proper method which gives output as:- \n", + "#['Python', 's', 'Programming', 'is', 'very', 'easy', 'to', 'learn'] in str.\n", + "import re\n", + "str = \"Python's Programming: is very easy to learn\"\n", + "output=re.split(r\"\\W+\",str)\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "id": "LxGl1o7px6Vi" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Python Programming: is very easy to learn\n" + ] + } + ], + "source": [ + "#Write the regular expression and use proper method which gives output as:- Python Programming: is very easy to learn in str.\n", + "import re\n", + "str = \"Python's Programming: is very easy to learn\"\n", + "output=re.split(r\"'\\w\",str)\n", + "print(\"\".join(output))" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "id": "9uIpb614x6Yb" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['peter', 'per', 'picked', 'peck', 'pickled', 'peppers']\n" + ] + } + ], + "source": [ + "#Write the regular expression and use proper method which Retrieves all words starting with p.\n", + "#Output:- ['peter', 'per', 'picked', 'peck', 'pickled', 'peppers']\n", + "import re\n", + "str = \"peter giper picked a peck of pickled peppers\"\n", + "output=re.findall(r\"p\\w+\",str)\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "id": "N0vFkePdx6bN" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['peter', 'picked', 'peck', 'pickled', 'peppers']\n" + ] + } + ], + "source": [ + "#Write the regular expression and use proper method which Retrieves all words starting with p except 'per' which is not a separate word.\n", + "#Output:- ['peter', 'picked', 'peck', 'pickled', 'peppers']\n", + "import re\n", + "str = \"peter giper picked a peck of pickled peppers\"\n", + "output=re.findall(r\"\\bp\\w+\",str)\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "id": "mbiypO3yx6eE" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['8th', '11th']\n" + ] + } + ], + "source": [ + "#Write the regular expression and use proper method which Retrieves all words starting with a digit.\n", + "#Output:- ['8th', '11th']\n", + "import re\n", + "str = 'The election in delhi will be held on 8th and result for the same will be declared on 11th'\n", + "output=re.findall(r\"\\d\\w+\",str)\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "id": "6ZUWWd6bx6g8" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['peter', 'giper']\n" + ] + } + ], + "source": [ + "#Write the regular expression and use proper method which Retrieves all words having 5 characters.\n", + "#Output:- ['peter', 'giper']\n", + "import re\n", + "str = \"peter giper picked a peck of pickled peppers\"\n", + "output=re.findall(r\"\\b\\w{5}\\b\",str)\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "id": "syuXvEXox6jt" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Retrieving', 'words', 'having', 'least', 'characters']\n" + ] + } + ], + "source": [ + "#Write the regular expression and use proper method which Retrieves all words having at least 4 characters.\n", + "#Output:- ['Retrieving', 'words', 'having', 'least', 'characters']\n", + "import re\n", + "str = \"Retrieving all words having at least 4 characters\"\n", + "output=re.findall(r\"\\b\\w{4,}\\b\",str)\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "id": "MtXo1Hf8x6ma" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['all', 'words', 'least']\n" + ] + } + ], + "source": [ + "#Write the regular expression and use proper method which Retrieves all words having at characters between 3 to 5 words.\n", + "#Output:- ['all', 'words', 'least']\n", + "import re\n", + "str = \"Retrieving all words having at least 4 characters\"\n", + "output=re.findall(r\"\\b\\w{3,5}\\b\",str)\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "id": "RlvMct7xx6pE" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['8', '11']\n" + ] + } + ], + "source": [ + "#Write the regular expression and use proper method which Retrieves only digits from the string.\n", + "#Output:- ['8', '11']\n", + "import re\n", + "str = 'The election in delhi will be held on 8 and result for the same will be declared on 11'\n", + "output=re.findall(r\"\\d+\",str)\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": { + "id": "wdD4cSyJx6us" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "characters\n" + ] + } + ], + "source": [ + "#Write the regular expression and use proper method which Retrieves last word if starts with c from the given string.\n", + "#Output:- ['characters']\n", + "import re\n", + "str = \"Retrieving all words having at least 4 characters\"\n", + "output=re.search(r\"c\\w+$\",str)\n", + "if output : \n", + " print(output.group())\n", + "else : \n", + " print(\"No such word found\")" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "id": "HsVGzJVbx6sl" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['1234567890']\n" + ] + } + ], + "source": [ + "#Write the regular expression and use proper method which Retrieves a phone number from the given string.\n", + "#Output:- 1234567890\n", + "import re\n", + "str = \"Learnbay : 1234567890\"\n", + "output=re.findall(r\"\\d+\",str)\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "id": "LvG3jTtvyQKk" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Learnbay : ']\n" + ] + } + ], + "source": [ + "##Write the regular expression and use proper method which Etracts name from the string but not number.\n", + "#Output:- Learnbay : \n", + "import re\n", + "str = \"Learnbay : 1234567890\"\n", + "output=re.findall(r\"\\D+\",str)\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": { + "id": "drArGXgKyQNz" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['anil', 'akhil', 'anant', 'ankur']\n" + ] + } + ], + "source": [ + "##Write the regular expression and use proper method which Retrieves name starting with 'an' or 'ak'.\n", + "#Output:- ['anil', 'akhil', 'anant', 'ankur']\n", + "import re\n", + "str = 'anil akhil anant abhi arun arati arundhati abhijit ankur'\n", + "output=re.findall(r\"a[nk]\\w+\",str)\n", + "print(output)\n" + ] + } + ], + "metadata": { + "colab": { + "name": "Regular_Expression.ipynb", + "provenance": [] + }, + "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.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +}