diff --git a/r.ipynb b/r.ipynb new file mode 100644 index 0000000..b66e5d4 --- /dev/null +++ b/r.ipynb @@ -0,0 +1,388 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "d9c9e526", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "import re\n", + "str = 'cat mat bat rat'\n", + "output=re.match(r\"c\\w\\w\",str)\n", + "\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "ef870eb7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "import re\n", + "str = 'cat mat bat rat'\n", + "output=re.search(r\"r\\w\\w\",str)\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "49f6f805", + "metadata": {}, + "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", + "out=re.findall(\"m\\w\\w\",str)\n", + "print(out)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "1f3284dd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Python', 's', 'Programming', '', 'is', 'very', 'easy', 'to', 'learn']\n" + ] + } + ], + "source": [ + "import re\n", + "str = \"Python's Programming: is very easy to learn\"\n", + "print(re.split(\"\\W\",str))" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "2c361dac", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Python', 'Programming: is very easy to learn']\n" + ] + } + ], + "source": [ + "# 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", + "print(re.split(\"'s \",str))" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "f11c7359", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['peter', 'per', 'picked', 'peck', 'pickled', 'peppers']\n" + ] + } + ], + "source": [ + "#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{2,}\\b\",str)\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "b289aed9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['peter', 'picked', 'peck', 'pickled', 'peppers']\n" + ] + } + ], + "source": [ + "#Retrieve all words starting with p except 'per' which is not a separate word.\n", + "import re\n", + "str = \"peter giper picked a peck of pickled peppers\"\n", + "\n", + "output=re.findall(r\"p\\w{3,}\\b\",str)\n", + "\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "cb0ab297", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['8th', '11th']\n" + ] + } + ], + "source": [ + "#Retrieves all words starting with a digit.\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(\"\\d{1,2}\\w\\w\",str)\n", + "print(output)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "1d97ca0b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['peter', 'giper']\n" + ] + } + ], + "source": [ + "#Retrieve all words having 5 characters.\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": 11, + "id": "5923bcba", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Retrieving', 'words', 'having', 'least', 'characters']\n" + ] + } + ], + "source": [ + "#Retrieve all words having at least 4 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": 12, + "id": "a4b9892e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['all', 'words', 'least']\n" + ] + } + ], + "source": [ + "#Retrieve all words having at characters between 3 to 5 words.\n", + "\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": 13, + "id": "41da455c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['8', '11']\n" + ] + } + ], + "source": [ + "# Retrieve only digits from the string.\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", + "out=re.findall(r\"\\b\\d{1,2}\\b\",str)\n", + "print(out)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "848fba27", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "#Retrieve last word if starts with c from the given string.\n", + "import re\n", + "str = \"Retrieving all words having at least 4 characters\"\n", + "st1=r\"c\\w{1,}$\"\n", + "nstr=re.search(st1,str)\n", + "print(nstr)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "46b8e789", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['1234567890']\n" + ] + } + ], + "source": [ + "#Retrieve a phone number from the given string.\n", + "import re\n", + "str = \"Learnbay : 1234567890\"\n", + "out=re.findall(r\"\\b\\d{10}\\b\",str)\n", + "print(out)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "ea300f1e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Learnbay : \n" + ] + } + ], + "source": [ + "#Extracs name from the string but not number\n", + "import re\n", + "str = \"Learnbay : 1234567890\"\n", + "st=r\"\\b\\D{1,}\\b\"\n", + "nst=\"\".join(re.findall(st,str))\n", + "print(nst)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "6d30a8c4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "anil akhil anant ankur\n" + ] + } + ], + "source": [ + "#Retrieve name starting with 'an' or 'ak'.\n", + "import re\n", + "str = 'anil akhil anant abhi arun arati arundhati abhijit ankur'\n", + "st1=r\"\\ba[n,k]\\w{1,}\\b\"\n", + "\n", + "nstr=\" \".join(re.findall(st1,str))\n", + "print(nstr)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "39b95d37", + "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.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}