{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## ``Q8.`` ## \n", "\n", "## ```Using Regular Expressions, develop a Python program to``` ## \n", "\n", "## ```a) Identify a word with a sequence of one upper case letter followed by lower case letters. ``` ## \n", "\n", "## ```b) Find all the patterns of '1(0+)1' in a given string. ``` ## \n", "\n", "## ```c) Match a word containing 'z' followed by one or more o's.``` ## \n", "\n", "## ```Prompt the user for input. ``` ## " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## -----------------------------------------------------------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Approach to identify a sequence of one upper case letter followed by lower case letters**\n", "\n", "To check if the sequence of one upper case letter followed by lower case letters we use regular expression ` [A-Z]+[a-z]+$` \n", "\n", "**Approach to find the patterns of `1(0+)1` in a given string** \n", "\n", "A string contains patterns of the form `1(0+)1` where `(0+)` represents any non-empty consecutive sequence of 0's. \n", "\n", "First compile a pattern which follows `1(0+)1` using re.compile(regex) method. Search a first sub-string in original string which follows `1(0+)1` pattern using `pattern.search(string)` method.\n", "\n", "`substr = pattern.search(string)` returns `None` if it doesn't find the given regex as sub-string in original string otherwise it returns first matched sub-string which follows `1(0+)1` pattern. \n", "\n", "`substr.start()` gives us starting index of matched regex and `substr.end()` gives us ending index of matched regex.\n", "Whenever we find regex as sub-string then increase count by 1 and again search for given regex starting from ending index of previous sub-string. *__The patterns are allowed to overlap.__*\n", "\n", "**Approach to match a word consisting of `z` followed by one or more `o's`**\n", "\n", "Compile a pattern which follows `zo+\\w*`, that matches a word which contains `'z'` followed by one or more `o's`. \n", "\n", "Then pass a string to the `findall()` method. This method returns the list of the matched strings. If the length of this list is equal to zero then it doesn't contain a matched string.\n", "\n", "`\\w` `->` `represents any letter, numeric digit, or the underscore character.` \n", "\n", "`*` `->` `means zero or more occurrence of the character.` \n", "\n", "`+` `->` `means one or more occurrence of the character.` " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## -----------------------------------------------------------------------------------------------------------------------------" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 --> Identify a word with a sequence of one upper case letter followed by lower case letters\n", "2 --> Find all the patterns of '1(0+)1' in a given string\n", "3 --> Match a word containing 'z' followed by one or more o's\n", "4 --> Exit the program\n", "Enter a number to perform any of the operation: 1\n", "\n", "\n", "Enter a string with a sequence of Upper and Lower case letters: Arunachalpradesh\n", "\n", "\n", "String pattern match success \n", "\n", "1 --> Identify a word with a sequence of one upper case letter followed by lower case letters\n", "2 --> Find all the patterns of '1(0+)1' in a given string\n", "3 --> Match a word containing 'z' followed by one or more o's\n", "4 --> Exit the program\n", "Enter a number to perform any of the operation: 1\n", "\n", "\n", "Enter a string with a sequence of Upper and Lower case letters: madhyaPradesh\n", "\n", "\n", "String pattern match success \n", "\n", "1 --> Identify a word with a sequence of one upper case letter followed by lower case letters\n", "2 --> Find all the patterns of '1(0+)1' in a given string\n", "3 --> Match a word containing 'z' followed by one or more o's\n", "4 --> Exit the program\n", "Enter a number to perform any of the operation: 1\n", "\n", "\n", "Enter a string with a sequence of Upper and Lower case letters: MadhyaPradesh\n", "\n", "\n", "String pattern match success \n", "\n", "1 --> Identify a word with a sequence of one upper case letter followed by lower case letters\n", "2 --> Find all the patterns of '1(0+)1' in a given string\n", "3 --> Match a word containing 'z' followed by one or more o's\n", "4 --> Exit the program\n", "Enter a number to perform any of the operation: 1\n", "\n", "\n", "Enter a string with a sequence of Upper and Lower case letters: andhrapradesh\n", "\n", "\n", "String fails the pattern \n", "\n", "1 --> Identify a word with a sequence of one upper case letter followed by lower case letters\n", "2 --> Find all the patterns of '1(0+)1' in a given string\n", "3 --> Match a word containing 'z' followed by one or more o's\n", "4 --> Exit the program\n", "Enter a number to perform any of the operation: 2\n", "\n", "\n", "Enter a string in the form of 1(0+)1 pattern: 10101010001111\n", "\n", "\n", "The number of times the pattern appears in the string is 4 \n", "\n", "1 --> Identify a word with a sequence of one upper case letter followed by lower case letters\n", "2 --> Find all the patterns of '1(0+)1' in a given string\n", "3 --> Match a word containing 'z' followed by one or more o's\n", "4 --> Exit the program\n", "Enter a number to perform any of the operation: 2\n", "\n", "\n", "Enter a string in the form of 1(0+)1 pattern: 110\n", "\n", "\n", "The number of times the pattern appears in the string is 0 \n", "\n", "1 --> Identify a word with a sequence of one upper case letter followed by lower case letters\n", "2 --> Find all the patterns of '1(0+)1' in a given string\n", "3 --> Match a word containing 'z' followed by one or more o's\n", "4 --> Exit the program\n", "Enter a number to perform any of the operation: 3\n", "\n", "\n", "Enter a string: zoo\n", "\n", "\n", "String pattern match success \n", "\n", "1 --> Identify a word with a sequence of one upper case letter followed by lower case letters\n", "2 --> Find all the patterns of '1(0+)1' in a given string\n", "3 --> Match a word containing 'z' followed by one or more o's\n", "4 --> Exit the program\n", "Enter a number to perform any of the operation: 3\n", "\n", "\n", "Enter a string: zip\n", "\n", "\n", "No match \n", "\n", "1 --> Identify a word with a sequence of one upper case letter followed by lower case letters\n", "2 --> Find all the patterns of '1(0+)1' in a given string\n", "3 --> Match a word containing 'z' followed by one or more o's\n", "4 --> Exit the program\n", "Enter a number to perform any of the operation: 4\n", "\n", "\n" ] } ], "source": [ "import re\n", "\n", "\n", "# Function to Find all the patterns of \"1(0+)1\" in a given string\n", "def check_uc_lc_pattern(user_input):\n", " # regex\n", " pattern = re.compile(\"[A-Z]+[a-z]+$\")\n", "\n", " # searching pattern\n", " if pattern.search(user_input):\n", " print(\"String pattern match success \\n\")\n", " else:\n", " print(\"String fails the pattern \\n\")\n", "\n", "\n", "def count_pattern(user_input):\n", " # search regex '10+1' in original string search() function return first occurrence\n", " # of regex '10+1' otherwise None '10+1' means sub-string starting and ending with 1\n", " # and at least 1 or more zeros in between\n", " count = 0\n", " pattern = re.compile(\"10+1\")\n", " substr = pattern.search(user_input)\n", "\n", " # search for regex in original string until we are done with complete string\n", " while substr != None:\n", " # if we find any occurrence then increase count by 1\n", " count = count + 1\n", "\n", " # find next occurrence just after previous sub-string for first occurrence of the pattern\n", " user_input = user_input[(substr.end() - 1) :]\n", " substr = pattern.search(user_input)\n", " print(f\"The number of times the pattern appears in the string is {count} \\n\")\n", "\n", "\n", "def z_followed_by_o(user_input):\n", " # Regex \\w * zo+\\w * will match text that contains 'z', followed by one or more 'o'\n", " pattern = re.compile(\"zo+\\w*\")\n", "\n", " # The findall() method returns all matching strings of the regex pattern\n", " match_object = pattern.findall(user_input)\n", "\n", " # If length of match_object is not equal to zero then it contains matched string\n", " if len(match_object) != 0:\n", " print(\"String pattern match success \\n\")\n", " else:\n", " print(\"No match \\n\")\n", "\n", "\n", "def menu():\n", " while True:\n", " print(\n", " \"1 --> Identify a word with a sequence of one upper case letter followed by lower case letters\"\n", " )\n", " print(\"2 --> Find all the patterns of '1(0+)1' in a given string\")\n", " print(\"3 --> Match a word containing 'z' followed by one or more o's\")\n", " print(\"4 --> Exit the program\")\n", " choice = int(input(\"Enter a number to perform any of the operation: \"))\n", " print(\"\\n\")\n", " if choice == 1:\n", " user_input = input(\n", " \"Enter a string with a sequence of Upper and Lower case letters: \"\n", " )\n", " print(\"\\n\")\n", " check_uc_lc_pattern(user_input)\n", " elif choice == 2:\n", " user_input = input(\"Enter a string in the form of 1(0+)1 pattern: \")\n", " print(\"\\n\")\n", " count_pattern(user_input)\n", " elif choice == 3:\n", " user_input = input(\"Enter a string: \")\n", " print(\"\\n\")\n", " z_followed_by_o(user_input)\n", " else:\n", " break\n", "\n", "\n", "# Main\n", "if __name__ == \"__main__\":\n", " menu()" ] } ], "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.7.3" }, "pycharm": { "stem_cell": { "cell_type": "raw", "metadata": { "collapsed": false }, "source": [] } } }, "nbformat": 4, "nbformat_minor": 1 }