Question

IN PYTHON. We have seen examples of using the python re library. In this practice, you...

IN PYTHON. We have seen examples of using the python re library. In this practice, you need to write python code and complete following tasks. (You can decide whether you want to define functions and how to organize your code. It is for your own practice.)

1). Define 10 string variables that follows the requirement given an integer, a float, a double, a float end with letter f (4.321f), Capital Letters( followed by small case letters, followed by digits, Exactly 3 digits, followed by 2 letters

2). Write down the regular expressions for these patterns

3). For each string: use if-elif-else and re library to test which pattern matches this string.

4). Print out formatted output messages for the pattern matching results. For example: Str1=”22.11” Test this string against different regular expression patterns until finding a match When a match is found, print out a message “22.11 matches the pattern: A double.”

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

import re

#to check integer
def isInteger(s):
    try:
        return isinstance(int(s), int)
    except:
        return False

#to get float
def isFloat(s):
    return False if (re.match('^\d+?\.\d+?f{1}$', s) is None) else True

#to get double string
def isdouble(s):
    try:
        return isinstance(float(s), float)
    except:
        return False

#To get capital string
def isCapital(s):
    return False if re.match('^[A-Z]+[a-z]+(\d{3})+([a-zA-Z]{2})$', s) is None else True

#to check str type and print
def checkTypeOfStr(s):
    if(isInteger(s)):
        print(s + " matches the pattern: A Integer")
        return
    elif(isdouble(s)):
        print(s + " matches the pattern: A Double")
        return
    elif(isFloat(s)):
        print(s + " matches the pattern: A Float")
        return
    elif(isCapital(s)):
        print(s + " matches the pattern: A Capital Letters")
        return
    else:
        print(s + " matches the pattern: A String")
        return

def main():
    #samples
    str1 = "10"
    str2 = "10.12"
    str3 = "10.1234f"
    str4 = "3123123"
    str5 = "0.34534f"
    str6 = "ADEeresad445eF"
    str7 = "2352345234523452345234"
    str8 = "2352345.234523452345234"
    str9 = "2352345.45234f"
    str10 = "DEMOAttached445eF"
    checkTypeOfStr(str1)
    checkTypeOfStr(str2)
    checkTypeOfStr(str3)
    checkTypeOfStr(str4)
    checkTypeOfStr(str5)
    checkTypeOfStr(str6)
    checkTypeOfStr(str7)
    checkTypeOfStr(str8)
    checkTypeOfStr(str9)
    checkTypeOfStr(str10)
main()


(sdev) (base) L-156168432:ai_drive PR951365$ python gatcha_camera.py 10 matches the pattern: A Integer 10.12 matches the patt

Add a comment
Know the answer?
Add Answer to:
IN PYTHON. We have seen examples of using the python re library. In this practice, you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Preliminaries For this lab you will be working with regular expressions in Python. Various functions for...

    Preliminaries For this lab you will be working with regular expressions in Python. Various functions for working with regular expressions are available in the re module. Fortunately, Python makes it pretty easy to check if a string matches a particular pattern. At the top of the file we must import the re module: import re Then we can use the search() function to test whether a string matches a pattern. In the example below, the regular expression has been saved...

  • Use the Python “re” module to do the following: 1. Write a regular expression pattern that matche...

    Use the Python “re” module to do the following: 1. Write a regular expression pattern that matches string “March 1, 2019, Mar 1, 2019, March First, 2019, March First, 19”. No credit for not using special characters: “*,+,?, | and etc” to do the matching. 2. Write a regular expression pattern that matches strings representing trains. A single letter stands for each kind of car in a train: Engine, Caboose, Boxcar, Passenger car, and Dining car. There are four rules...

  • WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you...

    WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you will write functions to encrypt and decrypt messages using simple substitution ciphers. Your solution MUST include: a function called encode that takes two parameters: key, a 26-character long string that identifies the ciphertext mapping for each letter of the alphabet, in order; plaintext, a string of unspecified length that represents the message to be encoded. encode will return a string representing the ciphertext. a...

  • In the lectures about lists we have seen some Python code examples that involve the processing of...

    In the lectures about lists we have seen some Python code examples that involve the processing of lists containing weather statistics. The original source of the values shown in the slides was taken from part of the Environment Canada website that serves up historical data: http://climate.weather.gc.ca/historical_data/search_historic_data_e.html    Data can be provied by that website using the Comma Separated Value (CSV) format which is stored in text files normally using a CSV suffix. We will not work directly with such files in...

  • In PYTHON ONLY PLEASE You will use a dictionary in Python You only have string in Python so you have to use a string dat...

    In PYTHON ONLY PLEASE You will use a dictionary in Python You only have string in Python so you have to use a string data type Use a Switch statement.A switch statement is just another way of writing an IF statement. The user will enter f, s, j, or r from the keyboard for freshman, sophomore, junior, or senior. Use character data type for c++. Then you can say something like: char level ='f'; level=toupper(level); You would need a loop...

  • # File: mlRemoveBlanks # Author: ************ # Date: mm/dd/yyyy # Purpose: Practice rotate & AND to...

    # File: mlRemoveBlanks # Author: ************ # Date: mm/dd/yyyy # Purpose: Practice rotate & AND to remove spare blanks #-------------------------------------------------------------- # Write a MIPS assembler program to remove the extra blanks from # a string: # INPUT: "Two bee ore knot too Bea that" # PATTERN:00011000100011000010001100010000 # ROTATE: 00110001000110000100011000100000 # AND: 00010000000010000000001000000000 # RESULT:"Two bee ore knot too Bea that " .data .eqv SYS_PRINT_WORD 1 #word, byte, character .eqv SYS_PRINT_FLOAT 2 #float .eqv SYS_PRINT_DOUBLE 3 #double .eqv SYS_PRINT_TEXT 4 #text...

  • Intro to Programming and Logic: Python 2, Chapter 8 – Strings (so far have learned the...

    Intro to Programming and Logic: Python 2, Chapter 8 – Strings (so far have learned the way of a program, variables, expressions, statements, functions, interface design, conditional, recursion and iteration) Write a program that takes a string as a parameter. It will analyze the string and return True if it is a valid float number. It will return False if it is not a valid float value. Your function should return True in any of these cases: 0.17, .17, 5.27,...

  • HEy GUYS PLZ I WROTE THIS CODE BUT I WAS ASKED TO ADD SOME ASSERTION TESTS AND I HAVE NEVER DONE SUCH THING. CAN YOU GUY...

    HEy GUYS PLZ I WROTE THIS CODE BUT I WAS ASKED TO ADD SOME ASSERTION TESTS AND I HAVE NEVER DONE SUCH THING. CAN YOU GUYS HELPS ME OUT ON HOW TO TEST A SIMPLE CODE SINCE ITS A GUESSING GAME! THANK YOU. PYTHON import random # here it allows us to use the benefits of the functions random #function 1 which is just a screen organized wordings def screen): screeninstructions () name input("Your Name : ") print('Welcome, name, 'This...

  • Your mission in this programming assignment is to create a Python program that will take an...

    Your mission in this programming assignment is to create a Python program that will take an input file, determine if the contents of the file contain email addresses and/or phone numbers, create an output file with any found email addresses and phone numbers, and then create an archive with the output file as its contents.   Tasks Your program is to accomplish the following: ‐ Welcome the user to the program ‐ Prompt for and get an input filename, a .txt...

  • I have a python project that requires me to make a password saver. The major part...

    I have a python project that requires me to make a password saver. The major part of the code is already giving. I am having trouble executing option 2 and 3. Some guidance will be appreciated. Below is the code giving to me. import csv import sys #The password list - We start with it populated for testing purposes passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]] #The password file name to store the passwords to passwordFileName = "samplePasswordFile" #The encryption key for the caesar...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT