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
// Create and run a CPU-bound task | |
Task<long> heavyCalculationTask = Task.Run(() => { | |
long sum = 0; | |
for (int i = 0; i < 1000000; i++) { | |
sum += i; | |
} | |
return sum; | |
}); | |
// The main thread is not blocked and can do other work |
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
// Parallel loop for CPU-bound work | |
Parallel.For(0, 1000, i => { | |
// Perform a heavy calculation | |
double result = Math.Pow(i, 2); | |
// ... | |
}); |
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
using System; | |
using System.Threading; | |
public class MyClass | |
{ | |
public static void DoWork() | |
{ | |
Console.WriteLine("Worker thread is running..."); | |
Thread.Sleep(2000); // Simulate work | |
Console.WriteLine("Worker thread finished."); |
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
re.search(): test | |
re.match(): None | |
re.findall(): ['test', 'test'] | |
re.sub(): This is a sample. My phone number is 123-456-7890. This is another sample. | |
re.split(): ['This is a ', '. My phone number is 123-456-7890. This is another ', '.'] |
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 | |
text = "This is a test. My phone number is 123-456-7890. This is another test." | |
pattern = r"test" | |
replacement = "sample" | |
# 1. re.search() - Finds the first match anywhere in the string | |
search_match = re.search(pattern, text) | |
print(f"re.search(): {search_match.group() if search_match else 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
All captured dates: [('2025', '09', '04'), ('2026', '01', '15')] | |
First full match: 2025-09-04 | |
Captured year: 2025 | |
Captured month: 09 | |
Captured day: 04 |
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 | |
text = "Invoice for 2025-09-04 was paid. Next invoice due 2026-01-15." | |
# \d matches any digit (0-9) | |
# {4} matches exactly 4 digits | |
# The parentheses create capturing groups for year, month, and day | |
pattern = r"(\d{4})-(\d{2})-(\d{2})" | |
# Find all matches |
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
raw_data = [ | |
"Contact me at user.name@email.com or call me at (123) 456-7890. This is a note.", | |
"Invoice from customer@example.net, phone: 987.654.3210. ID #892", | |
"Sent from my mobile. My email is support@docsallover.io. Call me at 555-123-4567.", | |
"Random text with no useful info.", | |
"Our team can be reached at docsallover@corp.co, or by phone: +1-555-999-8888. " | |
] |
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
def extract_emails(data_list): | |
"""Finds all email addresses in a list of strings.""" | |
email_pattern = r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}" | |
found_emails = [] | |
for text in data_list: | |
emails = re.findall(email_pattern, text) | |
found_emails.extend(emails) | |
return found_emails | |
print("--- Emails Extracted ---") |
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
def standardize_phones(data_list): | |
"""Converts various phone formats to ###-###-####.""" | |
# Pattern to find common phone number formats with optional country code or punctuation. | |
phone_pattern = r"(\+?\d{1,3}[-. ]?)?\(?(\d{3})\)?[-. ]?(\d{3})[-. ]?(\d{4})" | |
# Replacement string using captured groups from the pattern | |
# The \1, \2, etc., reference the groups in the pattern. | |
replacement = r"(\2) \3-\4" | |
cleaned_data = [re.sub(phone_pattern, replacement, text) for text in data_list] |
NewerOlder