diff --git a/Raksha_Regex_assignment/Regex_assignment.ipynb b/Raksha_Regex_assignment/Regex_assignment.ipynb new file mode 100644 index 0000000..01fda51 --- /dev/null +++ b/Raksha_Regex_assignment/Regex_assignment.ipynb @@ -0,0 +1,400 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 3, + "id": "5befa6ac", + "metadata": {}, + "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", + "str1 = 'cat mat bat rat'\n", + "\n", + "output = re.findall(\"c\\w\\w\", str1)\n", + "\n", + "if output:\n", + " print(output)\n", + "else:\n", + " print('No such word found')" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "f538eb31", + "metadata": {}, + "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", + "\n", + "str1 = 'cat mat bat rat'\n", + "\n", + "output = re.findall(\"r\\w\\w\", str1)\n", + "\n", + "if output:\n", + " print(output)\n", + "else:\n", + " print('No such word found')" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "eea2b896", + "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", + "\n", + "str1 = 'cat mat bat rat man'\n", + "\n", + "output = re.findall(\"m\\w\\w\", str1)\n", + "\n", + "if output:\n", + " print(output)\n", + "else:\n", + " print('No such word found')" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "76ce3518", + "metadata": {}, + "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", + "\n", + "str1 = \"Python's Programming: is very easy to learn\"\n", + "\n", + "output = re.split(\"\\W\", str1)\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3724fd64", + "metadata": {}, + "outputs": [], + "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", + "str1 = \"Python's Programming: is very easy to learn\"\n", + "\n", + "#######################################################################################################" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "4e4223de", + "metadata": {}, + "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", + "\n", + "str1 = \"peter giper picked a peck of pickled peppers\"\n", + "output = re.findall( \"p\\w*\" , str1)\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "a0c2c73d", + "metadata": {}, + "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", + "\n", + "str1 = \"peter giper picked a peck of pickled peppers\"\n", + "output = re.findall( r\"\\bp\\w*\\b\" , str1)\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "3e6e4206", + "metadata": {}, + "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", + "\n", + "str1 = 'The election in delhi will be held on 8th and result for the same will be declared on 11th'\n", + "output = re.findall( \"\\d\\d*\\w*\" , str1) \n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "a563d97b", + "metadata": {}, + "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", + "\n", + "str1 = \"peter giper picked a peck of pickled peppers\"\n", + "output = re.findall( r\"\\b\\w{5}\\b\" , str1) \n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "e79b595f", + "metadata": {}, + "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", + "\n", + "str1 = \"Retrieving all words having at least 4 characters\"\n", + "output = re.findall( r\"\\b\\w{4,}\\b\" , str1)\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "cad45d18", + "metadata": {}, + "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", + "\n", + "str = \"Retrieving all words having at least 4 characters\"\n", + "output = re.findall( r\"\\b\\w{3,5}\\b\" , str1)\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "9f98ba23", + "metadata": {}, + "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", + "\n", + "str1 = 'The election in delhi will be held on 8 and result for the same will be declared on 11'\n", + "output = re.findall( \"\\d\\d*\" , str1) \n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3c415126", + "metadata": {}, + "outputs": [], + "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", + "\n", + "str1 = \"Retrieving all words having at least 4 characters\"\n", + "output = re.\n", + "\n", + "###################################################################################" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "04a53567", + "metadata": {}, + "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", + "\n", + "str1 = \"Learnbay : 1234567890\"\n", + "output = re.findall( \"\\d{10}\" , str1) \n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "9a38dc1e", + "metadata": {}, + "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", + "\n", + "str1 = \"Learnbay : 1234567890\"\n", + "output = re.findall( r\"\\b\\D*\\b\" , str1)\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "e284beaf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['anil', '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", + "\n", + "str1 = 'anil akhil anant abhi arun arati arundhati abhijit ankur'\n", + "output = re.findall( r\"\\ban\\w*\\b\" or \"\\bak\\w*\\b\" , str1)\n", + "print(output)\n", + "\n", + "##############################################################################################" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "18fb1c69", + "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 +}