WEEK 3:: PYTHON AUTOMATION REGEX(REGULAR EXPRESSION)

Ace your homework & exams now with Quizwiz!

Fix the regular expression used in the rearrange_name function so that it can match middle names, middle initials, as well as double surnames.

import re def rearrange_name(name): result = re.search(r"^([\w \.-]*), ([\w \.-]*)$", name) if result == None: return name return "{} {}".format(result[2], result[1]) name=rearrange_name("Kennedy, John F.") print(name)

Using the terminal, which of the following commands will correctly use grep to find the words "sling" and "sting" (assuming they are in our file, file.txt)?

user@ubuntu:~$ grep s.ing /usr/file.txt (In regex, a dot is a wildcard, so it can represent any character. This command will print both "sting" and "sling", if they are in the file.)

Which of the following demonstrates how regex (regular expressions) might be used?

Find strings of text that match a pattern (Regex is a search query that matches the regular expression pattern you've specified.)

The circumflex [^] and the dollar sign [$] are anchor characters. What do these anchor characters do in regex?

Match the start and the end of a line (the circumflex and the dollar sign specifically match the start and end of a line.)

What is grep?

A command line regex tool (The grep command is used to scan files for a string of characters occurring that fits a specified sequence.)

When using regex, some characters represent particular types of characters. Some examples are the dollar sign, the circumflex, and the dot wildcard. What are these characters collectively known as?

Special characters. (Special characters, sometimes called meta characters, give special meaning to the regular expression search syntax.)

We want to split a piece of text by either the word "a" or "the", as implemented in the following code. What is the resulting split list? re.split(r"the|a", "One sentence. Another one? And the last one!")

['One sentence. Ano', 'r one? And ', ' l', 'st one!'] This regular expression uses "the" and "a" as delimiters, no matter where they are in the text, even in the middle of other words like "Another" and "last".

Fill in the code to check if the text passed looks like a standard sentence, meaning that it starts with an uppercase letter, followed by at least some lowercase letters or a space, and ends with a period, question mark, or exclamation point.

import re def check_sentence(text): result = re.search(r"^[A-Z][ |a-z]*[.?\!]$", text) return result != None --------- print(check_sentence("Is this is a sentence?")) # True print(check_sentence("is this is a sentence?")) # False print(check_sentence("Hello")) # False print(check_sentence("1-2-3-GO!")) # False print(check_sentence("A star is born.")) # True

The convert_phone_number function checks for a U.S. phone number format: XXX-XXX-XXXX (3 digits followed by a dash, 3 more digits followed by a dash, and 4 digits), and converts it to a more formal format that looks like this: (XXX) XXX-XXXX. Fill in the regular expression to complete this function.

import re def convert_phone_number(phone): result = re.sub(r"\b(\d{3})-(\d{3})-(\d{4})\b",r"(\1) \2-\3", phone) return result ------------------ print(convert_phone_number("My number is 212-345-9999.")) # My number is (212) 345-9999. print(convert_phone_number("Please call 888-555-1234")) # Please call (888) 555-1234 print(convert_phone_number("123-123-12345")) # 123-123-12345 print(convert_phone_number("Phone number of Buckingham Palace is +44 303 123 7300")) # Phone number of Buckingham Palace is +44 303 123 7300

The repeating_letter_a function checks if the text passed includes the letter "a" (lowercase or uppercase) at least twice. For example, repeating_letter_a("banana") is True, while repeating_letter_a("pineapple") is False. Fill in the code to make this work.

import re def repeating_letter_a(text): result = re.search(r"[Aa].*[aA]", text) return result != None ------------------- print(repeating_letter_a("banana")) # True print(repeating_letter_a("pineapple")) # False print(repeating_letter_a("Animal Kingdom")) # True print(repeating_letter_a("A is for apple")) # True (Ans: [Aa] .*[Aa])

Fill in the code to check if the text passed contains the vowels a, e and i, with exactly one occurrence of any other character in between.

import re def check_aei (text): result = re.search(r"a.e.i", text) return result != None print(check_aei("academia")) # True print(check_aei("aerial")) # False print(check_aei("paramedic")) # True

Fill in the code to check if the text passed has at least 2 groups of alphanumeric characters (including letters, numbers, and underscores) separated by one or more whitespace characters.

import re def check_character_groups(text): result = re.search(r"[0-9]\w", text) return result != None ------------------- print(check_character_groups("One")) # False print(check_character_groups("123 Ready Set GO")) # True print(check_character_groups("username user_01")) # True print(check_character_groups("shopping_list: milk, bread, eggs.")) # False (ans: [0-9]\w

Fill in the code to check if the text passed contains punctuation symbols: commas, periods, colons, semicolons, question marks, and exclamation points.

import re def check_punctuation (text): result = re.search(r"[,.:;?!]", text) return result != None -------------------- print(check_punctuation("This is a sentence that ends with a period.")) # True print(check_punctuation("This is a sentence fragment without a period")) # False print(check_punctuation("Aren't regular expressions awesome?")) # True print(check_punctuation("Wow! We're really picking up some steam now!")) # True print(check_punctuation("End of the line")) # False

What does the "r" before the pattern string in re.search(r"Py.*n", sample.txt) indicate?

raw string ("Raw" strings just means the Python interpreter won't try to interpret any special characters and, instead, will just pass the string to the function as it is.)

When using regular expressions, which of the following expressions uses a reserved character that can represent any single character?

re.findall(f.n, text) (The dot (.) represents any single character.)

When capturing regex groups, what datatype does the groups method return?

a tuple ( Because a tuple is returned, we can access each index individually.)

The check_time function checks for the time format of a 12-hour clock, as follows: the hour is between 1 and 12, with no leading zero, followed by a colon, then minutes between 00 and 59, then an optional space, and then AM or PM, in upper or lower case. Fill in the regular expression to do that. How many of the concepts that you just learned can you use here?

import re def check_time(text): pattern = r"^[1-9][0-2]?:[0-5][0-9] ?[am|pm|AM|PM]" result = re.search(pattern, text) return result != None ----------------- print(check_time("12:45pm")) # True print(check_time("9:59 AM")) # True print(check_time("6:60am")) # False print(check_time("five o'clock")) # False

The check_web_address function checks if the text passed qualifies as a top-level web address, meaning that it contains alphanumeric characters (which includes letters, numbers, and underscores), as well as periods, dashes, and a plus sign, followed by a period and a character-only top-level domain such as ".com", ".info", ".edu", etc. Fill in the regular expression to do that, using escape characters, wildcards, repetition qualifiers, beginning and end-of-line characters, and character classes.

import re def check_web_address(text): pattern = r"^\S+\.[a-zA-Z]+$" result = re.search(pattern, text) return result != None -------------------- print(check_web_address("gmail.com")) # True print(check_web_address("www@google")) # False print(check_web_address("www.Coursera.org")) # True print(check_web_address("web-address.com/homepage")) # False print(check_web_address("My_Favorite-Blog.US")) # True

Fill in the code to check if the text passed includes a possible U.S. zip code, formatted as follows: exactly 5 digits, and sometimes, but not always, followed by a dash with 4 more digits. The zip code needs to be preceded by at least one space, and cannot be at the start of the text.

import re def check_zip_code (text): result = re.search(r"[ ]\d{5}|[ ]\d{5}-\d{4}", text) return result != None --------------------- print(check_zip_code("The zip codes for New York are 10001 thru 11104.")) # True print(check_zip_code("90210 is a TV show")) # False print(check_zip_code("Their address is: 123 Main Street, Anytown, AZ 85258-0001.")) # True print(check_zip_code("The Parliament of Canada is at 111 Wellington St, Ottawa, ON K1A0A9.")) # False

The contains_acronym function checks the text for the presence of 2 or more characters or digits surrounded by parentheses, with at least the first character in uppercase (if it's a letter), returning True if the condition is met, or False otherwise. For example, "Instant messaging (IM) is a set of communication technologies used for text-based communication" should return True since (IM) satisfies the match conditions." Fill in the regular expression in this function:

import re def contains_acronym(text): pattern = r"\([A-Z1-9][a-zA-Z1-9]*\)" result = re.search(pattern, text) return result != None ------------ print(contains_acronym("Instant messaging (IM) is a set of communication technologies used for text-based communication")) # True print(contains_acronym("American Standard Code for Information Interchange (ASCII) is a character encoding standard for electronic communication")) # True print(contains_acronym("Please do NOT enter without permission!")) # False print(contains_acronym("PostScript is a fourth-generation programming language (4GL)")) # True print(contains_acronym("Have fun using a self-contained underwater breathing apparatus (Scuba)!")) # True

Add to the regular expression used in the extract_pid function, to return the uppercase message in parenthesis, after the process id.

import re def extract_pid(log_line): regex = r"\[(\d+)\]\: (\w+)" result = re.search(regex, log_line) if result is None: return None return "{} ({})".format(result[1], result[2]) -------------- print(extract_pid("July 31 07:51:48 mycomputer bad_process[12345]: ERROR Performing package upgrade")) # 12345 (ERROR) print(extract_pid("99 elephants in a [cage]")) # None print(extract_pid("A string that also has numbers [34567] but no uppercase message")) # None print(extract_pid("July 31 08:08:08 mycomputer new_process[67890]: RUNNING Performing backup")) # 67890 (RUNNING)

The long_words function returns all words that are at least 7 characters. Fill in the regular expression to complete this function.

import re def long_words(text): pattern = r"\w{7,}" result = re.findall(pattern, text) return result ----------- print(long_words("I like to drink coffee in the morning.")) # ['morning'] print(long_words("I also have a taste for hot chocolate in the afternoon.")) # ['chocolate', 'afternoon'] print(long_words("I never drink tea late at night.")) # []

The multi_vowel_words function returns all words with 3 or more consecutive vowels (a, e, i, o, u). Fill in the regular expression to do that.

import re def multi_vowel_words(text): pattern = r"(\w+[a,e,i,o,u]{3,}\w+)" result = re.findall(pattern, text) return result --------- print(multi_vowel_words("Life is beautiful")) # ['beautiful'] print(multi_vowel_words("Obviously, the queen is courageous and gracious.")) # ['Obviously', 'queen', 'courageous', 'gracious'] print(multi_vowel_words("The rambunctious children had to sit quietly and await their delicious dinner.")) # ['rambunctious', 'quietly', 'delicious'] print(multi_vowel_words("The order of a data queue is First In First Out (FIFO)")) # ['queue'] print(multi_vowel_words("Hello world!")) # []

The transform_comments function converts comments in a Python script into those usable by a C compiler. This means looking for text that begins with a hash mark (#) and replacing it with double slashes (//), which is the C single-line comment indicator. For the purpose of this exercise, we'll ignore the possibility of a hash mark embedded inside of a Python command, and assume that it's only used to indicate a comment. We also want to treat repetitive hash marks (##), (###), etc., as a single comment indicator, to be replaced with just (//) and not (#//) or (//#). Fill in the parameters of the substitution method to complete this function:

import re def transform_comments(line_of_code): result = re.sub(r"[\#]{1,}", "//",line_of_code) return result ----------- print(transform_comments("### Start of program")) # Should be "// Start of program" print(transform_comments(" number = 0 ## Initialize the variable")) # Should be " number = 0 // Initialize the variable" print(transform_comments(" number += 1 # Increment the variable")) # Should be " number += 1 // Increment the variable" print(transform_comments(" return(number)")) # Should be " return(number)"

Which of the following is NOT a function of the Python regex module?

re.grep() (The grep command utilizes regular expressions on Linux, but is not a part of the standard re Python module.)

Rather than using the index() function of the string module, we can use regular expressions, which are more flexible. After importing the regular expression module re, what regex function might be used instead of standard methods?

re.search() (Using the re module provides more robust solutions, not limited to just re.search().)

We're working with a CSV file, which contains employee information. Each record has a name field, followed by a phone number field, and a role field. The phone number field contains U.S. phone numbers, and needs to be modified to the international format, with "+1-" in front of the phone number. Fill in the regular expression, using groups, to use the transform_record function to do that.

import re def transform_record(record): new_record = re.sub(r",(\d{3})",r",+1-\1",record) return new_record ---------- print(transform_record("Sabrina Green,802-867-5309,System Administrator")) # Sabrina Green,+1-802-867-5309,System Administrator print(transform_record("Eli Jones,684-3481127,IT specialist")) # Eli Jones,+1-684-3481127,IT specialist print(transform_record("Melody Daniels,846-687-7436,Programmer")) # Melody Daniels,+1-846-687-7436,Programmer print(transform_record("Charlie Rivera,698-746-3357,Web Developer")) # Charlie Rivera,+1-698-746-3357,Web Developer

What does the plus character [+] do in regex?

matches one or more occurences of the character before it. (The plus character [+], matches one or more occurrences of the character that comes before it.


Related study sets

Community/ Public Health Theory Exam 1 Review Questions

View Set

ATI Chapter 21: Medications for Anxiety and Trauma-and Stressor-Related Disorders

View Set

Section 7: System Development Life Cycle

View Set

115 PrepU Ch. 21 Assessment of Cardiovascular Function

View Set

فيزياء الفصل الثانى

View Set

Chapter 26 Assessing Male Genitalia Rectum Prep U

View Set