This file contains 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 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 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 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 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 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 |
This file contains 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 repeating_letter_a(text): | |
result = re.search(r"[Aa].*a", 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 |
This file contains 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_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 |
This file contains 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 os | |
import csv | |
# Create a file with data in it | |
def create_file(filename): | |
with open(filename, "w") as file: | |
file.write("name,color,type\n") | |
file.write("carnation,pink,annual\n") | |
file.write("daffodil,yellow,perennial\n") | |
file.write("iris,blue,perennial\n") |
This file contains 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 os | |
import csv | |
# Create a file with data in it | |
def create_file(filename): | |
with open(filename, "w") as file: | |
file.write("name,color,type\n") | |
file.write("carnation,pink,annual\n") | |
file.write("daffodil,yellow,perennial\n") | |
file.write("iris,blue,perennial\n") |