This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.")) # [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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 |