diff --git a/12_09_practice.py b/12_09_practice.py deleted file mode 100644 index 7914563..0000000 --- a/12_09_practice.py +++ /dev/null @@ -1,29 +0,0 @@ -#Biggie Size - Given a list, write a function that changes all positive numbers in the list to "big". Example: make_it_big([-1, 3, 5, -5]) returns that same list, #changed to [-1, "big", "big", -5]. - -#Count Positives - Given a list of numbers, create a function to replace last value with number of positive values. Example, count_positives([-1,1,1,1]) changes list #to [-1,1,1,3] and returns it. (Note that zero is not considered to be a positive number). - -#SumTotal - Create a function that takes a list as an argument and returns the sum of all the values in the list. For example sum_total([1,2,3,4]) should return 10 - -#Average - Create a function that takes a list as an argument and returns the average of all the values in the list. For example multiples([1,2,3,4]) should return #2.5 - -#Length - Create a function that takes a list as an argument and returns the length of the list. For example length([1,2,3,4]) should return 4 - -#Minimum - Create a function that takes a list as an argument and returns the minimum value in the list. If the passed list is empty, have the function return false. #For example minimum([1,2,3,4]) should return 1; minimum([-1,-2,-3]) should return -3. -# -#Maximum - Create a function that takes a list as an argument and returns the maximum value in the list. If the passed list is empty, have the function return false. #For example maximum([1,2,3,4]) should return 4; maximum([-1,-2,-3]) should return -1. - -#Ultimateaalyze - Create a function that takes a list as an argument and returns a dictionary that has the sumTotal, average, minimum, maximum ad length of the list. - -#ReverseList - Create a function that takes a list as a argument and return a list in a reversed order. Do this without creating a empty temporary list. For example #reverse([1,2,3,4]) should return [4,3,2,1]. This challenge is known to appear during basic technical interviews. - -#Ispalindrome- Given a string, write a python function to check if it is palindrome or not. A string is said to be palindrome if the reverse of the string is the same as string. For example, “radar” is a palindrome, but “radix” is not a palindrome. - -#Fizzbuzz- Create a function that will print numbers from 1 to 100, with certain exceptions: - #If the number is a multiple of 3, print “Fizz” instead of the number. - #If the number is a multiple of 5, print “Buzz” instead of the number. - #If the number is a multiple of 3 and 5, print “FizzBuzz” instead of the number. - -#Fibonacci- The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, #starting from 0 and 1. That is, - #F(0) = 0, F(1) = 1 - #F(n) = F(n - 1) + F(n - 2), for n > 1. - #Create a function that accepts any number and will create a sequence based on the fibonacci sequence. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..a1971c8 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +#Regex Practice with Jupyter Notebook diff --git a/regex_excercises.ipynb b/regex_excercises.ipynb new file mode 100644 index 0000000..6e4d9b3 --- /dev/null +++ b/regex_excercises.ipynb @@ -0,0 +1,1341 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "mtZQDWxxos3J" + }, + "source": [ + "# 30 REGULAR EXPRESSION EXERCISES!" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6jEYHJH3os3Q" + }, + "source": [ + "Regex, you never know when they might come in handy. It's one of the \"good programmer\"'s fundamental yet a few people actually masters them.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "metadata": { + "id": "lbrLwTw4r9jM" + }, + "outputs": [], + "source": [ + "import datetime" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": { + "id": "ghj1l7sQos3R", + "trusted": true + }, + "outputs": [], + "source": [ + "import re" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_tO-OHUBos3T" + }, + "source": [ + "1) Write a Python program to check that a string contains only a certain set of characters (in this case a-z, A-Z and 0-9)." + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "cXaMTykzos3V", + "outputId": "a73c9558-3ac4-44e9-ad0f-685a8f627983", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n" + ] + } + ], + "source": [ + "# Solution\n", + "def is_allowed_specific_char(text):\n", + " # Define a regex pattern\n", + " pattern = r'[a-zA-Z][0-9]'\n", + " # Use the pattern to search for ....\n", + " match = re.search(pattern, text)\n", + " if match:\n", + " return 'True' \n", + " else:\n", + " return 'False'\n", + " \n", + "a = print(is_allowed_specific_char(\"ABCDEFabcdef123450\")) # True\n", + "b = print(is_allowed_specific_char(\"*&%@#!}{\")) # False" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2oae-B8tos3W" + }, + "source": [ + "2) Write a Python program that matches a string that has an a followed by zero or more b's" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "metadata": { + "id": "1LHEOOMkos3X", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Found a match!\n", + "Found a match!\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'a(b*)'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + "print(text_match(\"ac\")) # Found a match!\n", + "print(text_match(\"abc\")) # Found a match!\n", + "print(text_match(\"abbc\")) # Found a match!\n", + "print(text_match(\"bbc\")) # Not matched" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AQRTCmJZos3Z" + }, + "source": [ + "3) Write a Python program that matches a string that has an a followed by one or more b's" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "metadata": { + "id": "H1X2NPQYos3a", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Found a match!\n", + "Not matched!\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'a(b+)'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + "print(text_match(\"ab\")) # Found a match!\n", + "print(text_match(\"abc\")) # Found a match!\n", + "print(text_match(\"ac\")) # Not matched\n", + "print(text_match(\"bbc\")) # Not matched" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "E902uTcRos3a" + }, + "source": [ + "4) Write a Python program that matches a string that has an a followed by zero or one 'b'" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "metadata": { + "id": "xeva0fRNos3b", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Found a match!\n", + "Found a match!\n", + "Found a match!\n", + "Found a match!\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'a(b){0,1}'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + " \n", + "print(text_match(\"ab\")) # Found a match!\n", + "print(text_match(\"abc\")) # Found a match!\n", + "print(text_match(\"abbc\")) # Found a match!\n", + "print(text_match(\"aabbc\")) # Found a match!\n", + "print(text_match(\"ac\")) # Found a match!\n", + "print(text_match(\"bbc\")) # Not matched" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "whnPaW2kos3c" + }, + "source": [ + "5) Write a Python program that matches a string that has an a followed by three 'b'" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "metadata": { + "id": "0c_7S3h_os3d", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Found a match!\n", + "Not matched!\n", + "Not matched!\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'a(b){3}'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + "print(text_match(\"abbb\")) # Found a match!\n", + "print(text_match(\"aabbbbbc\")) # Found a match!\n", + "print(text_match(\"aabbc\")) # Not matched\n", + "print(text_match(\"ac\")) # Not matched\n", + "print(text_match(\"bbc\")) # Not matched" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "V4AsjJiCos3e" + }, + "source": [ + "6) Write a Python program that matches a string that has an a followed by two to three 'b'." + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "metadata": { + "id": "VjA5CXi1os3e", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Not matched!\n", + "Found a match!\n", + "Found a match!\n", + "Not matched!\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'a(b){2,3}'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + "print(text_match(\"ab\")) # Not matched\n", + "print(text_match(\"aabbbbbc\")) # Found a match!\n", + "print(text_match(\"aabbc\")) # Found a match!\n", + "print(text_match(\"ac\")) # Not matched\n", + "print(text_match(\"bbc\")) # Not matched" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kX7sLRavos3f" + }, + "source": [ + "7) Write a Python program to find sequences of lowercase letters joined with a underscore." + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "rbxk35n7os3g", + "outputId": "3ddca986-1bd6-45f1-902c-eee85e8853e8", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Not matched!\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'^[a-z]+_{1}[a-z]+'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + "print(text_match(\"aab_cbbbc\")) # Found a match!\n", + "print(text_match(\"aab_Abbbc\")) # Not matched\n", + "print(text_match(\"Aaab_abbbc\")) # Not matched" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "H4sat-Rfos3g" + }, + "source": [ + "8) Write a Python program to find the sequences of one upper case letter followed by lower case letters." + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "metadata": { + "id": "oeUiVHGVos3h", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Not matched!\n", + "Found a match!\n", + "Found a match!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'([A-Z]{1}[a-z]{1,})'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + "print(text_match(\"aab_cbbbc\")) # Not matched\n", + "print(text_match(\"aab_Abbbc\")) # Found a match!\n", + "print(text_match(\"Aaab_abbbc\")) # Found a match!" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RgNBl7aEos3h" + }, + "source": [ + "9) Write a Python program that matches a string that has an 'a' followed by anything, ending in 'b'." + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "metadata": { + "id": "YbCsos7Jos3i", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Not matched!\n", + "Not matched!\n", + "Found a match!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'a{1}.+b{1}$'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + "print(text_match(\"aabbbbd\")) # Not matched\n", + "print(text_match(\"aabAbbbc\")) # Not matched\n", + "print(text_match(\"accddbbjjjb\")) # Found a match!" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0oHZnx0Gos3i" + }, + "source": [ + "10) Write a Python program that matches a word at the beginning of a string." + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": { + "id": "1tlZiGMaos3j", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'^\\w+'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + "\n", + "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", + "print(text_match(\" The quick brown fox jumps over the lazy dog.\")) # Not matched" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mG0jspZFos3j" + }, + "source": [ + "11) Write a Python program that matches a word at the end of string, with optional punctuation." + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "metadata": { + "id": "cmhnhbZnos3j", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Not matched!\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'\\w+\\S*?$'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + " \n", + "\n", + "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", + "print(text_match(\"The quick brown fox jumps over the lazy dog. \")) # Not matched\n", + "print(text_match(\"The quick brown fox jumps over the lazy dog \")) # Not matched" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "DBgbyYHXos3k" + }, + "source": [ + "12) Write a Python program that matches a word containing 'z'" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": { + "id": "WxuMRyCbos3k", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'\\w+z{1}\\w+'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + " \n", + "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", + "print(text_match(\"Python Exercises.\")) # Not matched" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "V_9_nk8Kos3l" + }, + "source": [ + "13) Write a Python program that matches a word containing 'z', not at the start or end of the word." + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "metadata": { + "id": "VEhCCh7yos3l", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'\\w+z{1}\\w+'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", + "print(text_match(\"Python Exercises.\")) # Not matched" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "L0lV07OCos3m" + }, + "source": [ + "14) Write a Python program to match a string that contains only upper and lowercase letters, numbers, and underscores." + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "metadata": { + "id": "E9NcoWDhos3m", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Not matched!\n", + "Found a match!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'^\\w+$'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Not matched\n", + "print(text_match(\"Python_Exercises_1\")) # Found a match!" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "EvKJ8WOoos3n" + }, + "source": [ + "15) Write a Python program where a string will start with a specific number. " + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "metadata": { + "id": "nWp5QysTos3n", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n" + ] + } + ], + "source": [ + "# Solution\n", + "def match_num(text):\n", + " pattern = r'^[5]\\w*'\n", + " if re.search(pattern, text):\n", + " return 'True'\n", + " else:\n", + " return'False'\n", + " \n", + "print(match_num('5-2345861')) # True\n", + "print(match_num('6-2345861')) # False" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "r_I1cbvios3t" + }, + "source": [ + "16) Write a Python program to remove leading zeros from an IP address" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "metadata": { + "id": "cRLX4hc3os3u", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "216.8.94.196\n" + ] + } + ], + "source": [ + "# Solution\n", + "def rewrite_ip(ip1, ip2):\n", + " ip2 = re.sub('\\.[0]*', '.', ip1)\n", + " return ip2\n", + "\n", + "ip3 = \"216.08.094.196\"\n", + "ip4 = \"\"\n", + "string = rewrite_ip(ip3, ip4)\n", + "print(string) # 216.8.94.196" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZCn9r3o2os3u" + }, + "source": [ + "17) Write a Python program to check for a number at the end of a string." + ] + }, + { + "cell_type": "code", + "execution_count": 110, + "metadata": { + "id": "Q5t9U6n8os3u", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ] + } + ], + "source": [ + "# Solution\n", + "def end_num(text):\n", + " pattern = r'\\d$'\n", + " if re.search(pattern, text):\n", + " return 'True'\n", + " else:\n", + " return'False'\n", + "\n", + "print(end_num('abcdef')) # False\n", + "print(end_num('abcdef6')) # True" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0tM4eW8Wos3v" + }, + "source": [ + "18) Write a Python program to search the numbers (0-9) of length between 1 to 3 in a given string. " + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "metadata": { + "id": "7QGwzVxDos3v", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "12\n", + "13\n", + "345\n" + ] + } + ], + "source": [ + "# Solution\n", + "def print_digits(string, pattern):\n", + " pattern = r'(\\d{1,3})'\n", + " string2 = string.split() \n", + " for i in string2:\n", + " if re.search(pattern, i):\n", + " print(i.replace(',', ''))\n", + "\n", + "\n", + "string3 = \"Exercises number 1, 12, 13, and 345 are important\"\n", + "pattern2 = \"\"\n", + "print_digits(string3, pattern2)\n", + "# Number of length 1 to 3\n", + "# 1\n", + "# 12\n", + "# 13\n", + "# 345" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "43nNb7d_os3v" + }, + "source": [ + "19) Write a Python program to search some literals strings in a string. \n", + "Sample text : 'The quick brown fox jumps over the lazy dog.'\n", + "Searched words : 'fox', 'dog', 'horse'" + ] + }, + { + "cell_type": "code", + "execution_count": 112, + "metadata": { + "id": "KIVHxWN9os3w", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Searching for \"fox\" in \"The quick brown fox jumps over the lazy dog.\"->\n", + "Matched!\n", + "Searching for \"dog\" in \"The quick brown fox jumps over the lazy dog.\"->\n", + "Matched!\n", + "Searching for \"horse\" in \"The quick brown fox jumps over the lazy dog.\"->\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def print_match(patterns, text):\n", + " for i in patterns:\n", + " print('Searching for ' +'\"' +str(i) +'\"' +' in ' +'\"' +text +'\"' +'->')\n", + " if re.search(i, text): \n", + " print('Matched!')\n", + " else:\n", + " print('Not matched!')\n", + "\n", + "\n", + "patterns = [ 'fox', 'dog', 'horse' ]\n", + "text = 'The quick brown fox jumps over the lazy dog.'\n", + "print_match(patterns, text)\n", + "# Searching for \"fox\" in \"The quick brown fox jumps over the lazy dog.\" ->\n", + "# Matched!\n", + "# Searching for \"dog\" in \"The quick brown fox jumps over the lazy dog.\" ->\n", + "# Matched!\n", + "# Searching for \"horse\" in \"The quick brown fox jumps over the lazy dog.\" ->\n", + "# Not Matched!" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XjdROmMhos3w" + }, + "source": [ + "20) Write a Python program to search a literals string in a string and also find the location within the original string where the pattern occurs\n", + "\n", + "Sample text : 'The quick brown fox jumps over the lazy dog.'\n", + "Searched words : 'fox'" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "metadata": { + "id": "vWViImYios3w", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found \"fox\" in \"The quick brown fox jumps over the lazy dog.\" from 16 to 19\n" + ] + } + ], + "source": [ + "# Solution\n", + "def print_match_location(pattern, text):\n", + " match1 = re.search(r'(f{1}\\w+)', text)\n", + " if match1:\n", + " start = match1.start()\n", + " end = match1.end() \n", + " print('Found ' +'\"' +pattern +'\"'+' in ' +' \"The quick brown fox jumps over the lazy dog.\" ' +' from ' +str(start) +' to ' +str(end))\n", + " else:\n", + " print('No match')\n", + " \n", + "\n", + "\n", + "pattern = 'fox'\n", + "text = 'The quick brown fox jumps over the lazy dog.'\n", + "print_match_location(pattern, text)\n", + "# Found \"fox\" in \"The quick brown fox jumps over the lazy dog.\" from 16 to 19" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lLRotk-Oos3x" + }, + "source": [ + "21) Write a Python program to find the substrings within a string.\n", + "\n", + "Sample text :\n", + "\n", + "'Python exercises, PHP exercises, C# exercises'\n", + "\n", + "Pattern :\n", + "\n", + "'exercises'\n", + "\n", + "Note: There are three instances of exercises in the input string." + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "metadata": { + "id": "XU8613rTos3x", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found \"exercises\"\n", + "Found \"exercises\"\n", + "Found \"exercises\"\n" + ] + } + ], + "source": [ + "# Solution\n", + "def find_all_matches(pattern, text):\n", + " text1 = text.split()\n", + " for i in text1:\n", + " match = re.search(r'(e{1}\\w+)', i)\n", + " if match:\n", + " print('Found ' +'\"' +pattern +'\"')\n", + "\n", + "\n", + "text = 'Python exercises, PHP exercises, C# exercises'\n", + "pattern = 'exercises'\n", + "find_all_matches(pattern, text)\n", + "# Found \"exercises\"\n", + "# Found \"exercises\"\n", + "# Found \"exercises\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uvAY6v7Oos3x" + }, + "source": [ + "22) Write a Python program to find the occurrence and position of the substrings within a string." + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "metadata": { + "id": "UFg6rXJnos3y", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found \"exercises\" at 7 16\n", + "Found \"exercises\" at 22 31\n", + "Found \"exercises\" at 36 45\n" + ] + } + ], + "source": [ + "# Solution\n", + "def find_all_matches_location(pattern, text):\n", + " for m in re.finditer(pattern, text):\n", + " print('Found ' +'\"' +pattern +'\"' +' at ' +str(m.start()) +' ' +str(m.end()))\n", + " \n", + "\n", + "text = 'Python exercises, PHP exercises, C# exercises'\n", + "pattern = 'exercises'\n", + "find_all_matches_location(pattern, text)\n", + "# Found \"exercises\" at 7:16\n", + "# Found \"exercises\" at 22:31\n", + "# Found \"exercises\" at 36:45" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Sb3zKxE-os3y" + }, + "source": [ + "23) Write a Python program to replace whitespaces with an underscore and vice versa." + ] + }, + { + "cell_type": "code", + "execution_count": 125, + "metadata": { + "id": "nUxR5Xvjos3z", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Python_Exercises\n", + "Pytho Exercises\n" + ] + } + ], + "source": [ + "text = 'Python Exercises'\n", + "\n", + "# Your code\n", + "text1 = re.sub('\\s', '_', text)\n", + "print(text1) # Python_Exercises\n", + "\n", + "\n", + "# Your code\n", + "text2 = re.sub('\\w\\_', ' ', text1)\n", + "print(text2) # Python Exercises" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "grqoW-pgos3z" + }, + "source": [ + "24) Write a Python program to extract year, month and date from a an url. " + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "metadata": { + "id": "K2-pacqmos3z", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('2016', '09', '02')]\n" + ] + } + ], + "source": [ + "# Solution\n", + "def extract_date(url):\n", + " return re.findall(r'/(\\d{4})/(\\d{1,2})/(\\d{1,2})/', url)\n", + " \n", + " \n", + "url1= \"https://www.washingtonpost.com/news/football-insider/wp/2016/09/02/odell-beckhams-fame-rests-on-one-stupid-little-ball-josh-norman-tells-author/\"\n", + "print(extract_date(url1)) # [('2016', '09', '02')]" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gyD5AZW7os3z" + }, + "source": [ + "25) Write a Python program to convert a date of yyyy-mm-dd format to dd-mm-yyyy format." + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "metadata": { + "id": "bo-jpYqZos30", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Original date in YYY-MM-DD Format: 2026-01-02\n", + "New date in DD-MM-YYYY Format: 02-01-2026\n" + ] + } + ], + "source": [ + "# Solution\n", + "def change_date_format(dt):\n", + " return re.sub(r'(\\d{4})-(\\d{1,2})-(\\d{1,2})', '\\\\3-\\\\2-\\\\1', dt)\n", + "\n", + "\n", + "dt1 = \"2026-01-02\"\n", + "print(\"Original date in YYY-MM-DD Format: \",dt1) # Original date in YYY-MM-DD Format: 2026-01-02\n", + "print(\"New date in DD-MM-YYYY Format: \",change_date_format(dt1)) # New date in DD-MM-YYYY Format: 02-01-2026\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IQ2SNL-gos30" + }, + "source": [ + "26) Write a Python program to match if any words from a list of words starting with letter 'P'." + ] + }, + { + "cell_type": "code", + "execution_count": 127, + "metadata": { + "id": "2hP4Twe0os31", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Python', 'PHP')\n" + ] + } + ], + "source": [ + "# Sample strings.\n", + "def print_words_starting_with_P(words):\n", + " for i in words:\n", + " match1 = re.match('(P\\w+)\\W(P\\w+)', i)\n", + " if match1:\n", + " print(match1.groups())\n", + "\n", + "words = [\"Python PHP\", \"Java JavaScript\", \"c c++\"]\n", + "print_words_starting_with_P(words) # ('Python', 'PHP')" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MEUMKMn0os31" + }, + "source": [ + "27) Write a Python program to separate and print the numbers of a given string." + ] + }, + { + "cell_type": "code", + "execution_count": 120, + "metadata": { + "id": "xG8c9PtUos32", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "# 10\n", + "# 20\n", + "# 30\n" + ] + } + ], + "source": [ + "# Solution\n", + "def print_all_numbers(text):\n", + " i = text.split()\n", + " for j in i:\n", + " match1 = re.search(r'\\d', j)\n", + " if match1:\n", + " print('# ' +j.replace(',', ''))\n", + " else:\n", + " pass\n", + "# Sample string.\n", + "text = \"Ten 10, Twenty 20, Thirty 30\"\n", + "print_all_numbers(text)\n", + "# 10\n", + "# 20\n", + "# 30" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "krdKu3Mvos32" + }, + "source": [ + "28) Write a Python program to find all words starting with 'a' or 'e' in a given string." + ] + }, + { + "cell_type": "code", + "execution_count": 121, + "metadata": { + "id": "3hsR5278os32", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['example', 'eates', 'an', 'ayList', 'apacity', 'elements', 'elements', 'are', 'en', 'added', 'ayList', 'and', 'ayList', 'ed', 'accordingly']\n", + "None\n" + ] + } + ], + "source": [ + "# Solution\n", + "def get_all_words_containing_a_or_e(text):\n", + " list1 = []\n", + " list1 = re.findall(r'[ae]\\w+', text)\n", + " print(list1)\n", + "\n", + " \n", + "# Input.\n", + "text = \"The following example creates an ArrayList with a capacity of 50 elements. Four elements are then added to the ArrayList and the ArrayList is trimmed accordingly.\"\n", + "print(get_all_words_containing_a_or_e(text))\n", + "# ['example', 'eates', 'an', 'ayList', 'apacity', 'elements', 'elements', 'are', 'en', 'added', 'ayList', 'and', 'ayList', 'ed', 'accordingly']" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CxfIZ5o9os33" + }, + "source": [ + "29) Write a Python program to separate and print the numbers and their position of a given string." + ] + }, + { + "cell_type": "code", + "execution_count": 131, + "metadata": { + "id": "XIuiRvmAos33", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "50 \n", + "Index Position: 62\n" + ] + } + ], + "source": [ + "# Solution\n", + "def print_all_numbers_and_their_position(text):\n", + " for i in re.finditer('(\\d{1,}\\s)', text):\n", + " print(i.group(0))\n", + " print('Index Position: ' +str(i.start()))\n", + " \n", + "\n", + "# Input.\n", + "text = \"The following example creates an ArrayList with a capacity of 50 elements. Four elements are then added to the ArrayList and the ArrayList is trimmed accordingly.\"\n", + "print_all_numbers_and_their_position(text)\n", + "# 50\n", + "# Index position: 62" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JguO0fUjos34" + }, + "source": [ + "30) Write a Python program to abbreviate 'Road' as 'Rd.' in a given string." + ] + }, + { + "cell_type": "code", + "execution_count": 123, + "metadata": { + "id": "VJE1Xm2Bos34", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "21 Ramkrishna Rd\n" + ] + } + ], + "source": [ + "# Solution\n", + "def abbreviate_road(street):\n", + " return re.sub('Road{1}', 'Rd', street)\n", + "\n", + "street = '21 Ramkrishna Road'\n", + "\n", + "print(abbreviate_road(street)) # 21 Ramkrishna Rd.\n" + ] + } + ], + "metadata": { + "colab": { + "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.11.0" + }, + "vscode": { + "interpreter": { + "hash": "8b70a4ea831fd5b9b770fab33cac3f0a285469b7491720cba6f6d634f19e8405" + } + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}