Skip to content

Instantly share code, notes, and snippets.

View Codehunter-py's full-sized avatar
🏠
Working from home

Ibrahim Musayev Codehunter-py

🏠
Working from home
View GitHub Profile
@Codehunter-py
Codehunter-py / multi_vowels_words.py
Last active November 2, 2023 11:07
The multi_vowel_words function returns all words with 3 or more consecutive vowels (a, e, i, o, u).
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."))
@Codehunter-py
Codehunter-py / extract_pid.py
Created March 6, 2022 16:36
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
@Codehunter-py
Codehunter-py / long_words.py
Last active May 30, 2022 10:13
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.")) # []
@Codehunter-py
Codehunter-py / rearrange_name.py
Created March 6, 2022 16:01
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)
@Codehunter-py
Codehunter-py / check_zip_code.py
Created February 20, 2022 19:08
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
@Codehunter-py
Codehunter-py / contains_acronym.py
Created February 20, 2022 19:06
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 …
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
@Codehunter-py
Codehunter-py / check_time.py
Created February 20, 2022 19:06
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.
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
@Codehunter-py
Codehunter-py / check_web_address.py
Created February 20, 2022 19:05
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.
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
@Codehunter-py
Codehunter-py / check_sentence.py
Created February 20, 2022 18:32
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
@Codehunter-py
Codehunter-py / alphanumeric.py
Created February 20, 2022 18:11
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