From d5267aa81e53778d3111030e4e6381a2b87043aa Mon Sep 17 00:00:00 2001 From: Dmytro Shumsky Date: Wed, 25 Oct 2023 16:08:17 +0300 Subject: [PATCH] add regex hw --- homework/week5/regex/regex.py | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 homework/week5/regex/regex.py diff --git a/homework/week5/regex/regex.py b/homework/week5/regex/regex.py new file mode 100644 index 0000000..b208c40 --- /dev/null +++ b/homework/week5/regex/regex.py @@ -0,0 +1,46 @@ +import re + +# Function to match a regular expression pattern in a string +def match_pattern(pattern, text): + """ + Match a regular expression pattern in a given text and return the result. + """ + matches = re.findall(pattern, text) + return matches + +# Function to find and replace using regular expressions +def find_and_replace(pattern, replacement, text): + """ + Find and replace all occurrences of a regular expression pattern in a given text with a replacement string. + """ + replaced_text = re.sub(pattern, replacement, text) + return replaced_text + +# Function to split a string using a regular expression +def split_text(pattern, text): + """ + Split a text into a list of substrings using a regular expression pattern as the delimiter. + """ + substrings = re.split(pattern, text) + return substrings + +# Example usage +if __name__ == "__main__": + # Example text + sample_text = "Hello, my email is john@example.com and my phone number is 123-456-7890." + + # 1. Matching a pattern + email_pattern = r'WRITE_HERE' + email_matches = match_pattern(email_pattern, sample_text) + print("Email matches:", email_matches) + + # 2. Finding and replacing + phone_pattern = r'WRITE_HERE' + replaced_text = find_and_replace(phone_pattern, "XXX-XXX-XXXX", sample_text) + print("Text after phone number replacement:") + print(replaced_text) + + # 3. Splitting text + words_pattern = r'WRITE_HERE' + word_list = split_text(words_pattern, sample_text) + print("Words in the text:", word_list)