🎯 empfohlene Sammlungen
Balanced sample collections from various categories for you to explore
Web Python Zeichenfolgenverarbeitung Beispiele
Web Python Zeichenfolgenverarbeitungsbeispiele einschließlich Zeichenfolgenaufteilung/verbindung, reguläre Ausdrücke und Zeichenfolgersersatz
💻 Zeichenfolgenaufteilung/verbindung python
🟢 simple
⭐⭐
Zeichenfolgen nach Trennzeichen aufteilen und mit Trennzeichen verbinden mit verschiedenen Methoden
⏱️ 20 min
🏷️ python, web, string processing, split, join
Prerequisites:
Basic Python, Strings
# Web Python String Split/Join Examples
# Comprehensive string splitting and joining operations
# 1. Basic Split Operations
from typing import List, Any
def split_by_space(text: str) -> List[str]:
"""
Split string by whitespace
Args:
text: Input string
Returns:
List of words
"""
return text.split()
def split_by_char(text: str, delimiter: str) -> List[str]:
"""
Split string by character delimiter
Args:
text: Input string
delimiter: Delimiter character
Returns:
List of parts
"""
return text.split(delimiter)
def split_by_char_limited(text: str, delimiter: str, max_splits: int) -> List[str]:
"""
Split string with maximum splits
Args:
text: Input string
delimiter: Delimiter character
max_splits: Maximum number of splits
Returns:
List of parts
"""
return text.split(delimiter, max_splits)
def split_lines(text: str, keep_ends: bool = False) -> List[str]:
"""
Split string into lines
Args:
text: Input string
keep_ends: Keep line endings
Returns:
List of lines
"""
if keep_ends:
return text.split('\n')
return text.splitlines()
def split_by_any(text: str, delimiters: str) -> List[str]:
"""
Split by any of multiple delimiters
Args:
text: Input string
delimiters: String of delimiter characters
Returns:
List of parts
"""
import re
return re.split(f'[{re.escape(delimiters)}]+', text)
# 2. Advanced Split Operations
def split_by_words(text: str) -> List[str]:
"""
Split into words (removing punctuation)
Args:
text: Input string
Returns:
List of words
"""
import re
return re.findall(r'\b\w+\b', text)
def split_by_sentences(text: str) -> List[str]:
"""
Split into sentences
Args:
text: Input string
Returns:
List of sentences
"""
import re
# Split by ., !, or ? followed by space or end
sentences = re.split(r'(?<=[.!?])\s+(?=[A-Z])', text)
return [s.strip() for s in sentences if s.strip()]
def split_and_keep(text: str, delimiter: str) -> List[str]:
"""
Split but keep delimiters
Args:
text: Input string
delimiter: Delimiter string
Returns:
List of parts with delimiters
"""
import re
pattern = f'({re.escape(delimiter)})'
return re.split(pattern, text)
def split_by_length(text: str, length: int) -> List[str]:
"""
Split string into fixed-length chunks
Args:
text: Input string
length: Chunk length
Returns:
List of chunks
"""
return [text[i:i + length] for i in range(0, len(text), length)]
def split_by_pattern(text: str, pattern: str) -> List[str]:
"""
Split by regex pattern
Args:
text: Input string
pattern: Regex pattern
Returns:
List of parts
"""
import re
return re.split(pattern, text)
def balanced_split(text: str, delimiter: str, num_parts: int) -> List[str]:
"""
Split into balanced parts
Args:
text: Input string
delimiter: Delimiter
num_parts: Number of parts
Returns:
List of balanced parts
"""
parts = text.split(delimiter)
size = len(parts) // num_parts
result = []
for i in range(num_parts):
if i == num_parts - 1:
result.append(delimiter.join(parts[i * size:]))
else:
result.append(delimiter.join(parts[i * size:(i + 1) * size]))
return result
# 3. Basic Join Operations
def join_with_char(strings: List[str], delimiter: str) -> str:
"""
Join strings with delimiter
Args:
strings: List of strings
delimiter: Delimiter string
Returns:
Joined string
"""
return delimiter.join(strings)
def join_with_space(strings: List[str]) -> str:
"""
Join strings with space
Args:
strings: List of strings
Returns:
Joined string
"""
return ' '.join(strings)
def join_with_newline(strings: List[str]) -> str:
"""
Join strings with newline
Args:
strings: List of strings
Returns:
Joined string
"""
return '\n'.join(strings)
def join_with_tab(strings: List[str]) -> str:
"""
Join strings with tab
Args:
strings: List of strings
Returns:
Joined string
"""
return '\t'.join(strings)
def join_empty(strings: List[str]) -> str:
"""
Join strings without separator
Args:
strings: List of strings
Returns:
Concatenated string
"""
return ''.join(strings)
# 4. Advanced Join Operations
def join_with_index(strings: List[str], template: str = '{i}. {s}') -> str:
"""
Join with index numbers
Args:
strings: List of strings
template: Format template (i=index, s=string)
Returns:
Joined string with indices
"""
return '\n'.join(template.format(i=i + 1, s=s) for i, s in enumerate(strings))
def join_as_pairs(strings: List[str], pair_delimiter: str = ': ', item_delimiter: str = '\n') -> str:
"""
Join as key-value pairs
Args:
strings: List of even length
pair_delimiter: Delimiter between pairs
item_delimiter: Delimiter between items
Returns:
Joined string
"""
pairs = []
for i in range(0, len(strings), 2):
if i + 1 < len(strings):
pairs.append(f"{strings[i]}{pair_delimiter}{strings[i + 1]}")
return item_delimiter.join(pairs)
def join_with_quotes(strings: List[str], quote: str = "'") -> str:
"""
Join with quotes around each string
Args:
strings: List of strings
quote: Quote character
Returns:
Joined string
"""
return ', '.join(f"{quote}{s}{quote}" for s in strings)
def join_sentences(sentences: List[str]) -> str:
"""
Join sentences properly
Args:
sentences: List of sentences
Returns:
Joined paragraph
"""
return ' '.join(s.strip() for s in sentences if s.strip())
def wrap_and_join(strings: List[str], width: int = 80, indent: str = '') -> str:
"""
Wrap and join text
Args:
strings: List of strings
width: Line width
indent: Indentation for each line
Returns:
Wrapped text
"""
import textwrap
text = ' '.join(strings)
wrapped = textwrap.fill(text, width=width)
if indent:
wrapped = '\n'.join(indent + line for line in wrapped.split('\n'))
return wrapped
# 5. Combined Operations
def split_and_join(text: str, split_delimiter: str, join_delimiter: str) -> str:
"""
Split and rejoin with different delimiter
Args:
text: Input string
split_delimiter: Original delimiter
join_delimiter: New delimiter
Returns:
Rejoined string
"""
return join_with_char(text.split(split_delimiter), join_delimiter)
def split_filter_join(text: str, delimiter: str, predicate) -> str:
"""
Split, filter, and rejoin
Args:
text: Input string
delimiter: Delimiter
predicate: Filter function
Returns:
Filtered and rejoined string
"""
parts = text.split(delimiter)
filtered = [p for p in parts if predicate(p)]
return delimiter.join(filtered)
def split_map_join(text: str, delimiter: str, func) -> str:
"""
Split, transform, and rejoin
Args:
text: Input string
delimiter: Delimiter
func: Transform function
Returns:
Transformed and rejoined string
"""
parts = text.split(delimiter)
mapped = [func(p) for p in parts]
return delimiter.join(mapped)
def split_trim_join(text: str, delimiter: str) -> str:
"""
Split, trim whitespace, and rejoin
Args:
text: Input string
delimiter: Delimiter
Returns:
Trimmed and rejoined string
"""
parts = text.split(delimiter)
trimmed = [p.strip() for p in parts]
return delimiter.join(trimmed)
def split_unique_join(text: str, delimiter: str) -> str:
"""
Split, remove duplicates, and rejoin
Args:
text: Input string
delimiter: Delimiter
Returns:
Unique and rejoined string
"""
parts = text.split(delimiter)
seen = set()
unique = []
for p in parts:
if p not in seen:
seen.add(p)
unique.append(p)
return delimiter.join(unique)
# 6. Specialized Operations
def parse_csv_line(line: str) -> List[str]:
"""
Parse CSV line (simple)
Args:
line: CSV line
Returns:
List of fields
"""
import re
# Handle quoted fields
pattern = r'(?:^|,)(?:"([^"]*)"|([^",]*))'
matches = re.findall(pattern, line)
return [m[0] if m[0] else m[1] for m in matches]
def join_as_csv(fields: List[Any]) -> str:
"""
Join as CSV line
Args:
fields: List of fields
Returns:
CSV string
"""
import csv
from io import StringIO
output = StringIO()
writer = csv.writer(output)
writer.writerow(fields)
return output.getvalue().rstrip('\n')
def parse_key_value(text: str, pair_delimiter: str = ',', kv_delimiter: str = '=') -> dict:
"""
Parse key-value pairs
Args:
text: Input string
pair_delimiter: Delimiter between pairs
kv_delimiter: Delimiter between key and value
Returns:
Dictionary
"""
result = {}
pairs = text.split(pair_delimiter)
for pair in pairs:
if kv_delimiter in pair:
key, value = pair.split(kv_delimiter, 1)
result[key.strip()] = value.strip()
return result
def join_key_value(pairs: dict, pair_delimiter: str = ', ', kv_delimiter: str = '=') -> str:
"""
Join key-value pairs
Args:
pairs: Dictionary
pair_delimiter: Delimiter between pairs
kv_delimiter: Delimiter between key and value
Returns:
Joined string
"""
return pair_delimiter.join(f"{k}{kv_delimiter}{v}" for k, v in pairs.items())
def create_table(rows: List[List[str]], header_delimiter: str = '|', row_delimiter: str = '\n') -> str:
"""
Create table from rows
Args:
rows: List of rows (each row is list of cells)
header_delimiter: Delimiter between cells
row_delimiter: Delimiter between rows
Returns:
Table string
"""
return row_delimiter.join(header_delimiter.join(row) for row in rows)
# 7. Utility Functions
def split_preserve_quotes(text: str, delimiter: str = ',') -> List[str]:
"""
Split preserving quoted sections
Args:
text: Input string
delimiter: Delimiter
Returns:
List of parts
"""
import re
pattern = f'{re.escape(delimiter)}(?=(?:[^"]*"[^"]*")*[^"]*$)'
return re.split(pattern, text)
def join_for_url(params: dict) -> str:
"""
Join parameters for URL
Args:
params: Dictionary of parameters
Returns:
URL query string
"""
return '&'.join(f"{k}={v}" for k, v in params.items())
def split_url_params(url: str) -> dict:
"""
Split URL parameters
Args:
url: URL string
Returns:
Dictionary of parameters
"""
from urllib.parse import urlparse, parse_qs
parsed = urlparse(url)
return {k: v[0] if len(v) == 1 else v for k, v in parse_qs(parsed.query).items()}
# Usage Examples
def demonstrate_string_split_join():
print("=== Web Python String Split/Join Examples ===\n")
# 1. Basic split
print("--- 1. Basic Split ---")
text = "Hello World Python Programming"
print(f"Text: {text}")
print(f"Split by space: {split_by_space(text)}")
print(f"Split by 'o': {split_by_char(text, 'o')}")
print(f"Split limited: {split_by_char_limited(text, ' ', 2)}")
# 2. Advanced split
print("\n--- 2. Advanced Split ---")
text = "word1,word2;word3|word4"
print(f"Text: {text}")
print(f"Split by delimiters: {split_by_any(text, ',;|')}")
text = "Hello world. How are you? I'm fine!"
print(f"\nText: {text}")
print(f"Split sentences: {split_by_sentences(text)}")
# 3. Basic join
print("\n--- 3. Basic Join ---")
words = ["Python", "is", "awesome"]
print(f"Words: {words}")
print(f"Join with space: '{join_with_space(words)}'")
print(f"Join with dash: '{join_with_char(words, '-')}'")
print(f"Join empty: '{join_empty(words)}'")
# 4. Advanced join
print("\n--- 4. Advanced Join ---")
items = ["apple", "banana", "cherry"]
print(f"Items: {items}")
print(f"With index:\n{join_with_index(items)}")
print(f"With quotes: {join_with_quotes(items)}")
# 5. Combined operations
print("\n--- 5. Combined Operations ---")
text = "apple, banana, cherry, date"
print(f"Original: {text}")
print(f"Split and join: '{split_and_join(text, ', ', '; ')}'")
text = " apple , banana , cherry "
print(f"\nOriginal: '{text}'")
print(f"Split trim join: '{split_trim_join(text, ',')}'")
# 6. Specialized
print("\n--- 6. Specialized Operations ---")
text = "name=John,age=30,city=NYC"
print(f"Text: {text}")
parsed = parse_key_value(text)
print(f"Parsed: {parsed}")
print(f"Joined back: {join_key_value(parsed, ', ')}")
# 7. CSV
print("\n--- 7. CSV Operations ---")
fields = ["John", "Doe", "30"]
csv_line = join_as_csv(fields)
print(f"CSV: {csv_line}")
print("\n=== All String Split/Join Examples Completed ===")
# Export functions
export { split_by_space, split_by_char, split_by_char_limited, split_lines }
export { split_by_any, split_by_words, split_by_sentences, split_and_keep }
export { split_by_length, split_by_pattern, balanced_split }
export { join_with_char, join_with_space, join_with_newline, join_with_tab, join_empty }
export { join_with_index, join_as_pairs, join_with_quotes, join_sentences, wrap_and_join }
export { split_and_join, split_filter_join, split_map_join, split_trim_join, split_unique_join }
export { parse_csv_line, join_as_csv, parse_key_value, join_key_value, create_table }
export { split_preserve_quotes, join_for_url, split_url_params }
export { demonstrate_string_split_join }
💻 Reguläre Ausdrücke python
🟡 intermediate
⭐⭐⭐
Reguläre Ausdrücke für Musterabgleich, Validierung, Suche und komplexe Zeichenfolgenoperationen verwenden
⏱️ 30 min
🏷️ python, web, string processing, regex
Prerequisites:
Intermediate Python, re module
# Web Python Regular Expression Examples
# Comprehensive regex pattern matching and string operations
# 1. Basic Pattern Matching
import re
from typing import List, Optional, Pattern, Match
def find_pattern(text: str, pattern: str) -> Optional[Match]:
"""
Find first occurrence of pattern
Args:
text: Input text
pattern: Regex pattern
Returns:
Match object or None
"""
return re.search(pattern, text)
def find_all_patterns(text: str, pattern: str) -> List[str]:
"""
Find all occurrences of pattern
Args:
text: Input text
pattern: Regex pattern
Returns:
List of matches
"""
return re.findall(pattern, text)
def match_pattern(text: str, pattern: str) -> bool:
"""
Check if text matches pattern at start
Args:
text: Input text
pattern: Regex pattern
Returns:
True if matches
"""
return bool(re.match(pattern, text))
def full_match(text: str, pattern: str) -> bool:
"""
Check if text fully matches pattern
Args:
text: Input text
pattern: Regex pattern
Returns:
True if full match
"""
return bool(re.fullmatch(pattern, text))
# 2. Search and Find
def search_with_flags(text: str, pattern: str, flags: int = 0) -> List[str]:
"""
Search with regex flags
Args:
text: Input text
pattern: Regex pattern
flags: Regex flags (re.I, re.M, re.S, etc.)
Returns:
List of matches
"""
return re.findall(pattern, text, flags)
def find_iter(text: str, pattern: str) -> List[Match]:
"""
Find all matches as iterator
Args:
text: Input text
pattern: Regex pattern
Returns:
List of match objects
"""
return list(re.finditer(pattern, text))
def find_overlap(text: str, pattern: str) -> List[str]:
"""
Find overlapping matches
Args:
text: Input text
pattern: Regex pattern
Returns:
List of overlapping matches
"""
matches = []
for i in range(len(text)):
m = re.match(pattern, text[i:])
if m:
matches.append(m.group())
return matches
def search_multiline(text: str, pattern: str) -> List[str]:
"""
Search in multiline mode
Args:
text: Input text
pattern: Regex pattern
Returns:
List of matches
"""
return re.findall(pattern, text, re.MULTILINE)
# 3. Validation Patterns
def validate_email(email: str) -> bool:
"""
Validate email address
Args:
email: Email string
Returns:
True if valid
"""
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$'
return bool(re.fullmatch(pattern, email))
def validate_phone(phone: str) -> bool:
"""
Validate phone number (US format)
Args:
phone: Phone string
Returns:
True if valid
"""
pattern = r'^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$'
return bool(re.fullmatch(pattern, phone))
def validate_url(url: str) -> bool:
"""
Validate URL
Args:
url: URL string
Returns:
True if valid
"""
pattern = r'^https?://[^\s/$.?#].[^\s]*$'
return bool(re.fullmatch(pattern, url))
def validate_ip(ip: str) -> bool:
"""
Validate IPv4 address
Args:
ip: IP string
Returns:
True if valid
"""
pattern = r'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
return bool(re.fullmatch(pattern, ip))
def validate_date(date: str, format: str = 'YYYY-MM-DD') -> bool:
"""
Validate date format
Args:
date: Date string
format: Date format pattern
Returns:
True if valid
"""
if format == 'YYYY-MM-DD':
pattern = r'^\d{4}-\d{2}-\d{2}$'
return bool(re.fullmatch(pattern, date))
return False
def validate_password(password: str) -> dict:
"""
Validate password strength
Args:
password: Password string
Returns:
Validation result dict
"""
result = {
'valid': True,
'errors': [],
'strength': 'weak'
}
if len(password) < 8:
result['errors'].append('Password must be at least 8 characters')
result['valid'] = False
if not re.search(r'[a-z]', password):
result['errors'].append('Password must contain lowercase letter')
result['valid'] = False
if not re.search(r'[A-Z]', password):
result['errors'].append('Password must contain uppercase letter')
result['valid'] = False
if not re.search(r'\d', password):
result['errors'].append('Password must contain digit')
result['valid'] = False
if not re.search(r'[!@#$%^&*(),.?":{}|<>]', password):
result['errors'].append('Password must contain special character')
result['valid'] = False
# Calculate strength
if result['valid']:
if len(password) >= 12:
result['strength'] = 'strong'
else:
result['strength'] = 'medium'
return result
def validate_username(username: str) -> bool:
"""
Validate username
Args:
username: Username string
Returns:
True if valid
"""
pattern = r'^[a-zA-Z0-9_]{3,20}$'
return bool(re.fullmatch(pattern, username))
def validate_hex_color(color: str) -> bool:
"""
Validate hex color code
Args:
color: Color string
Returns:
True if valid
"""
pattern = r'^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$'
return bool(re.fullmatch(pattern, color))
def validate_postal_code(code: str, country: str = 'US') -> bool:
"""
Validate postal code
Args:
code: Postal code string
country: Country code
Returns:
True if valid
"""
patterns = {
'US': r'^\d{5}(-\d{4})?$',
'UK': r'^[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}$',
'CA': r'^[A-Z]\d[A-Z] ?\d[A-Z]\d$',
'DE': r'^\d{5}$'
}
pattern = patterns.get(country, patterns['US'])
return bool(re.fullmatch(pattern, code))
# 4. Extraction Operations
def extract_emails(text: str) -> List[str]:
"""
Extract all email addresses
Args:
text: Input text
Returns:
List of emails
"""
pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
return re.findall(pattern, text)
def extract_urls(text: str) -> List[str]:
"""
Extract all URLs
Args:
text: Input text
Returns:
List of URLs
"""
pattern = r'https?://[^\s/$.?#][^\s]*'
return re.findall(pattern, text)
def extract_phone_numbers(text: str) -> List[str]:
"""
Extract all phone numbers
Args:
text: Input text
Returns:
List of phone numbers
"""
pattern = r'\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})'
return re.findall(pattern, text)
def extract_numbers(text: str) -> List[str]:
"""
Extract all numbers
Args:
text: Input text
Returns:
List of number strings
"""
return re.findall(r'-?\d+\.?\d*', text)
def extract_hashtags(text: str) -> List[str]:
"""
Extract all hashtags
Args:
text: Input text
Returns:
List of hashtags
"""
return re.findall(r'#\w+', text)
def extract_mentions(text: str) -> List[str]:
"""
Extract all @mentions
Args:
text: Input text
Returns:
List of mentions
"""
return re.findall(r'@\w+', text)
def extract_dates(text: str) -> List[str]:
"""
Extract dates in various formats
Args:
text: Input text
Returns:
List of date strings
"""
patterns = [
r'\d{4}-\d{2}-\d{2}', # YYYY-MM-DD
r'\d{2}/\d{2}/\d{4}', # MM/DD/YYYY
r'\d{1,2}\s+(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*\s+\d{4}'
]
dates = []
for pattern in patterns:
dates.extend(re.findall(pattern, text, re.IGNORECASE))
return dates
def extract_html_tags(text: str) -> List[str]:
"""
Extract HTML tags
Args:
text: Input text
Returns:
List of tags
"""
return re.findall(r'<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>', text)
# 5. Replace Operations
def replace_pattern(text: str, pattern: str, replacement: str) -> str:
"""
Replace all occurrences of pattern
Args:
text: Input text
pattern: Regex pattern
replacement: Replacement string
Returns:
Modified text
"""
return re.sub(pattern, replacement, text)
def replace_first(text: str, pattern: str, replacement: str) -> str:
"""
Replace first occurrence
Args:
text: Input text
pattern: Regex pattern
replacement: Replacement string
Returns:
Modified text
"""
return re.sub(pattern, replacement, text, count=1)
def replace_nth(text: str, pattern: str, replacement: str, n: int) -> str:
"""
Replace nth occurrence
Args:
text: Input text
pattern: Regex pattern
replacement: Replacement string
n: Occurrence number (1-based)
Returns:
Modified text
"""
count = 0
def repl(match):
nonlocal count
count += 1
if count == n:
return replacement
return match.group(0)
return re.sub(pattern, repl, text)
def replace_with_func(text: str, pattern: str, func) -> str:
"""
Replace using callback function
Args:
text: Input text
pattern: Regex pattern
func: Replacement function
Returns:
Modified text
"""
return re.sub(pattern, func, text)
# 6. Text Processing
def remove_extra_spaces(text: str) -> str:
"""
Remove extra whitespace
Args:
text: Input text
Returns:
Cleaned text
"""
return ' '.join(text.split())
def remove_special_chars(text: str, keep: str = '') -> str:
"""
Remove special characters
Args:
text: Input text
keep: Characters to keep
Returns:
Cleaned text
"""
pattern = f'[^a-zA-Z0-9\s{re.escape(keep)}]' if keep else '[^a-zA-Z0-9\s]'
return re.sub(pattern, '', text)
def remove_html_tags(text: str) -> str:
"""
Remove HTML tags
Args:
text: Input text
Returns:
Cleaned text
"""
return re.sub(r'<[^>]+>', '', text)
def censor_words(text: str, words: List[str]) -> str:
"""
Censor specific words
Args:
text: Input text
words: Words to censor
Returns:
Censored text
"""
for word in words:
pattern = r'\b' + re.escape(word) + r'\b'
text = re.sub(pattern, '*' * len(word), text, flags=re.IGNORECASE)
return text
def highlight_text(text: str, pattern: str, marker: str = '**') -> str:
"""
Highlight pattern matches
Args:
text: Input text
pattern: Search pattern
marker: Highlight markers
Returns:
Highlighted text
"""
return re.sub(f'({pattern})', f'{marker}\1{marker}', text, flags=re.IGNORECASE)
# 7. Code Pattern Matching
def find_python_variables(text: str) -> List[str]:
"""
Find Python variable names
Args:
text: Input text
Returns:
List of variable names
"""
return re.findall(r'\b[a-zA-Z_][a-zA-Z0-9_]*\b', text)
def find_python_functions(text: str) -> List[str]:
"""
Find Python function definitions
Args:
text: Input text
Returns:
List of function names
"""
return re.findall(r'def\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\(', text)
def find_css_colors(text: str) -> List[str]:
"""
Find CSS color codes
Args:
text: Input text
Returns:
List of color codes
"""
return re.findall(r'#[a-fA-F0-9]{6}|#[a-fA-F0-9]{3}\b', text)
def find_json_keys(text: str) -> List[str]:
"""
Find JSON keys
Args:
text: Input text
Returns:
List of keys
"""
return re.findall(r'"([^"]+)":\s*', text)
# 8. Advanced Patterns
def find_palindromes(text: str) -> List[str]:
"""
Find palindromes (case-insensitive)
Args:
text: Input text
Returns:
List of palindromes
"""
words = re.findall(r'\b\w+\b', text)
return [w for w in words if w.lower() == w[::-1].lower() and len(w) > 1]
def find_repeated_words(text: str) -> List[tuple]:
"""
Find repeated words
Args:
text: Input text
Returns:
List of (word, count) tuples
"""
words = re.findall(r'\b\w+\b', text.lower())
from collections import Counter
return [(word, count) for word, count in Counter(words).items() if count > 1]
def find_sentences_with_word(text: str, word: str) -> List[str]:
"""
Find sentences containing word
Args:
text: Input text
word: Word to search
Returns:
List of sentences
"""
pattern = r'[^.!?]*\b' + re.escape(word) + r'\b[^.!?]*[.!?]'
return re.findall(pattern, text, re.IGNORECASE)
# Usage Examples
def demonstrate_regex():
print("=== Web Python Regular Expression Examples ===\n")
# 1. Basic matching
print("--- 1. Basic Pattern Matching ---")
text = "The price is $123.45"
pattern = r'\$\d+\.\d{2}'
print(f"Text: {text}")
print(f"Pattern: {pattern}")
print(f"Find: {find_pattern(text, pattern)}")
# 2. Validation
print("\n--- 2. Validation ---")
email = "[email protected]"
print(f"Email: {email}")
print(f"Valid: {validate_email(email)}")
password = "MyPass123!"
result = validate_password(password)
print(f"\nPassword: {password}")
print(f"Valid: {result['valid']}")
print(f"Strength: {result['strength']}")
if result['errors']:
print(f"Errors: {result['errors']}")
# 3. Extraction
print("\n--- 3. Extraction ---")
text = "Contact us at [email protected] or [email protected]"
emails = extract_emails(text)
print(f"Text: {text}")
print(f"Emails: {emails}")
text = "Call us at (555) 123-4567 or 555.987.6543"
phones = extract_phone_numbers(text)
print(f"\nText: {text}")
print(f"Phone numbers: {phones}")
# 4. Text processing
print("\n--- 4. Text Processing ---")
text = "This has extra spaces"
print(f"Original: '{text}'")
print(f"Cleaned: '{remove_extra_spaces(text)}'")
text = "Hello <b>world</b>! How are you?"
print(f"\nOriginal: {text}")
print(f"No HTML: {remove_html_tags(text)}")
# 5. Replace
print("\n--- 5. Replace Operations ---")
text = "The color is red and red is nice"
print(f"Original: {text}")
print(f"Replace 'red' with 'blue': {replace_pattern(text, r'\bred\b', 'blue')}")
# 6. Code patterns
print("\n--- 6. Code Pattern Matching ---")
code = "def calculate(x, y): return x + y"
functions = find_python_functions(code)
print(f"Code: {code}")
print(f"Functions: {functions}")
# 7. Advanced
print("\n--- 7. Advanced Patterns ---")
text = "level refers to the level of the radar"
palindromes = find_palindromes(text)
print(f"Text: {text}")
print(f"Palindromes: {palindromes}")
repeated = find_repeated_words(text)
print(f"\nRepeated words: {repeated}")
print("\n=== All Regular Expression Examples Completed ===")
# Export functions
export { find_pattern, find_all_patterns, match_pattern, full_match }
export { search_with_flags, find_iter, find_overlap, search_multiline }
export { validate_email, validate_phone, validate_url, validate_ip, validate_date }
export { validate_password, validate_username, validate_hex_color, validate_postal_code }
export { extract_emails, extract_urls, extract_phone_numbers, extract_numbers }
export { extract_hashtags, extract_mentions, extract_dates, extract_html_tags }
export { replace_pattern, replace_first, replace_nth, replace_with_func }
export { remove_extra_spaces, remove_special_chars, remove_html_tags }
export { censor_words, highlight_text }
export { find_python_variables, find_python_functions, find_css_colors, find_json_keys }
export { find_palindromes, find_repeated_words, find_sentences_with_word }
export { demonstrate_regex }
💻 Zeichenfolgersatz python
🟡 intermediate
⭐⭐⭐
Verschiedene Zeichenfolgersatzoperationen durchführen einschließlich einfachem, bedingtem und Batch-Ersatz
⏱️ 25 min
🏷️ python, web, string processing, replace
Prerequisites:
Intermediate Python, Strings, re module
# Web Python String Replacement Examples
# Comprehensive string replacement operations and techniques
# 1. Basic Replacement
from typing import Dict, List, Callable, Any, Optional
import re
def replace_all(text: str, old: str, new: str) -> str:
"""
Replace all occurrences
Args:
text: Input string
old: Substring to replace
new: Replacement string
Returns:
Modified string
"""
return text.replace(old, new)
def replace_first(text: str, old: str, new: str) -> str:
"""
Replace first occurrence
Args:
text: Input string
old: Substring to replace
new: Replacement string
Returns:
Modified string
"""
index = text.find(old)
if index != -1:
return text[:index] + new + text[index + len(old):]
return text
def replace_last(text: str, old: str, new: str) -> str:
"""
Replace last occurrence
Args:
text: Input string
old: Substring to replace
new: Replacement string
Returns:
Modified string
"""
index = text.rfind(old)
if index != -1:
return text[:index] + new + text[index + len(old):]
return text
def replace_nth(text: str, old: str, new: str, n: int) -> str:
"""
Replace nth occurrence (1-based)
Args:
text: Input string
old: Substring to replace
new: Replacement string
n: Occurrence number
Returns:
Modified string
"""
count = 0
start = 0
while True:
index = text.find(old, start)
if index == -1:
break
count += 1
if count == n:
return text[:index] + new + text[index + len(old):]
start = index + len(old)
return text
# 2. Count-Based Replacement
def replace_n_times(text: str, old: str, new: str, n: int) -> str:
"""
Replace first n occurrences
Args:
text: Input string
old: Substring to replace
new: Replacement string
n: Number of replacements
Returns:
Modified string
"""
count = 0
result = []
start = 0
while count < n:
index = text.find(old, start)
if index == -1:
break
result.append(text[start:index])
result.append(new)
start = index + len(old)
count += 1
result.append(text[start:])
return ''.join(result)
def replace_if_count(text: str, old: str, new: str, condition: Callable[[int], bool]) -> str:
"""
Replace based on occurrence count
Args:
text: Input string
old: Substring to replace
new: Replacement string
condition: Function taking count, returns True to replace
Returns:
Modified string
"""
count = 0
result = []
start = 0
while True:
index = text.find(old, start)
if index == -1:
break
count += 1
if condition(count):
result.append(text[start:index])
result.append(new)
else:
result.append(text[start:index + len(old)])
start = index + len(old)
result.append(text[start:])
return ''.join(result)
# 3. Conditional Replacement
def replace_if_contains(text: str, old: str, new: str, condition: str) -> str:
"""
Replace if text contains condition
Args:
text: Input string
old: Substring to replace
new: Replacement string
condition: Condition string
Returns:
Modified string
"""
if condition in text:
return text.replace(old, new)
return text
def replace_if_length(text: str, old: str, new: str, min_length: int = 0, max_length: int = float('inf')) -> str:
"""
Replace based on text length
Args:
text: Input string
old: Substring to replace
new: Replacement string
min_length: Minimum length
max_length: Maximum length
Returns:
Modified string
"""
if min_length <= len(text) <= max_length:
return text.replace(old, new)
return text
def replace_with_condition(text: str, old: str, new: str, condition: Callable[[str], bool]) -> str:
"""
Replace based on custom condition
Args:
text: Input string
old: Substring to replace
new: Replacement string
condition: Condition function
Returns:
Modified string
"""
if condition(text):
return text.replace(old, new)
return text
# 4. Multiple Replacements
def replace_multiple(text: str, replacements: Dict[str, str]) -> str:
"""
Replace multiple substrings
Args:
text: Input string
replacements: Dictionary of {old: new}
Returns:
Modified string
"""
for old, new in replacements.items():
text = text.replace(old, new)
return text
def replace_multiple_sequential(text: str, replacements: List[tuple]) -> str:
"""
Replace sequentially (order matters)
Args:
text: Input string
replacements: List of (old, new) tuples
Returns:
Modified string
"""
for old, new in replacements:
text = text.replace(old, new)
return text
def replace_multiple_simultaneous(text: str, replacements: Dict[str, str]) -> str:
"""
Replace multiple simultaneously (avoid conflicts)
Args:
text: Input string
replacements: Dictionary of {old: new}
Returns:
Modified string
"""
# Create pattern that matches any of the strings
pattern = re.compile('|'.join(re.escape(key) for key in replacements.keys()))
def repl(match):
return replacements[match.group(0)]
return pattern.sub(repl, text)
# 5. Pattern-Based Replacement
def replace_pattern(text: str, pattern: str, replacement: str) -> str:
"""
Replace using regex pattern
Args:
text: Input string
pattern: Regex pattern
replacement: Replacement string
Returns:
Modified string
"""
return re.sub(pattern, replacement, text)
def replace_pattern_func(text: str, pattern: str, func: Callable) -> str:
"""
Replace using callback function
Args:
text: Input string
pattern: Regex pattern
func: Replacement function
Returns:
Modified string
"""
return re.sub(pattern, func, text)
def replace_case_insensitive(text: str, old: str, new: str) -> str:
"""
Replace ignoring case
Args:
text: Input string
old: Substring to replace
new: Replacement string
Returns:
Modified string
"""
return re.sub(re.escape(old), new, text, flags=re.IGNORECASE)
def replace_word_boundaries(text: str, old: str, new: str) -> str:
"""
Replace whole words only
Args:
text: Input string
old: Word to replace
new: Replacement word
Returns:
Modified string
"""
pattern = r'\b' + re.escape(old) + r'\b'
return re.sub(pattern, new, text)
# 6. Position-Based Replacement
def replace_at_position(text: str, start: int, end: int, replacement: str) -> str:
"""
Replace text at position
Args:
text: Input string
start: Start position
end: End position
replacement: Replacement string
Returns:
Modified string
"""
return text[:start] + replacement + text[end:]
def replace_at_line(text: str, line_num: int, replacement: str) -> str:
"""
Replace entire line
Args:
text: Input text (multiline)
line_num: Line number (1-based)
replacement: Replacement line
Returns:
Modified text
"""
lines = text.split('\n')
if 1 <= line_num <= len(lines):
lines[line_num - 1] = replacement
return '\n'.join(lines)
def replace_lines_matching(text: str, pattern: str, replacement: str) -> str:
"""
Replace lines matching pattern
Args:
text: Input text (multiline)
pattern: Line pattern
replacement: Replacement line
Returns:
Modified text
"""
lines = text.split('\n')
for i, line in enumerate(lines):
if re.search(pattern, line):
lines[i] = replacement
return '\n'.join(lines)
# 7. Template Replacement
def replace_placeholders(text: str, values: Dict[str, Any]) -> str:
"""
Replace {placeholder} with values
Args:
text: Template string
values: Dictionary of values
Returns:
Filled template
"""
return text.format(**values)
def replace_placeholders_braced(text: str, values: Dict[str, Any]) -> str:
"""
Replace ${placeholder} with values
Args:
text: Template string
values: Dictionary of values
Returns:
Filled template
"""
for key, value in values.items():
text = text.replace(' + key + ', str(value))
return text
def replace_percent_style(text: str, *args) -> str:
"""
Replace %s style placeholders
Args:
text: Template string
*args: Values
Returns:
Filled template
"""
return text % args
# 8. Advanced Replacement
def replace_except(text: str, old: str, new: str, except_positions: List[int]) -> str:
"""
Replace all except at positions
Args:
text: Input string
old: Substring to replace
new: Replacement string
except_positions: List of positions to skip
Returns:
Modified string
"""
result = []
start = 0
count = 0
while True:
index = text.find(old, start)
if index == -1:
break
count += 1
if count not in except_positions:
result.append(text[start:index])
result.append(new)
else:
result.append(text[start:index + len(old)])
start = index + len(old)
result.append(text[start:])
return ''.join(result)
def replace_between(text: str, start_marker: str, end_marker: str, replacement: str) -> str:
"""
Replace text between markers
Args:
text: Input string
start_marker: Start marker
end_marker: End marker
replacement: Replacement string
Returns:
Modified string
"""
pattern = re.escape(start_marker) + r'.*?' + re.escape(end_marker)
return re.sub(pattern, start_marker + replacement + end_marker, text, flags=re.DOTALL)
def replace_with_fallback(text: str, old: str, new: str, fallback: str) -> str:
"""
Replace with fallback if old not found
Args:
text: Input string
old: Substring to replace
new: Replacement string
fallback: Fallback string
Returns:
Modified string
"""
if old in text:
return text.replace(old, new)
return text + fallback
def replace_with_confirmation(text: str, old: str, new: str) -> tuple:
"""
Replace with confirmation
Args:
text: Input string
old: Substring to replace
new: Replacement string
Returns:
(modified_text, count) tuple
"""
count = text.count(old)
modified = text.replace(old, new)
return (modified, count)
# 9. Smart Replacement
def auto_capitalize_after_period(text: str) -> str:
"""
Capitalize after periods
Args:
text: Input string
Returns:
Modified string
"""
def repl(match):
return match.group(1) + match.group(2).upper()
return re.sub(r'([.!?]\s+)([a-z])', repl, text)
def fix_double_spaces(text: str) -> str:
"""
Fix multiple spaces
Args:
text: Input string
Returns:
Modified string
"""
return re.sub(r' +', ' ', text)
def fix_double_punctuation(text: str) -> str:
"""
Fix repeated punctuation
Args:
text: Input string
Returns:
Modified string
"""
return re.sub(r'([.!?])\1+', r'\1', text)
def normalize_whitespace(text: str) -> str:
"""
Normalize all whitespace
Args:
text: Input string
Returns:
Modified string
"""
return ' '.join(text.split())
def smart_quotes(text: str) -> str:
"""
Convert straight quotes to smart quotes
Args:
text: Input string
Returns:
Modified string
"""
# Opening quotes
text = re.sub(r'"(\w)', r'"\1', text)
# Closing quotes
text = re.sub(r'(\w)"', r'\1"', text)
return text
# 10. Batch Operations
def batch_replace(files_content: Dict[str, str], replacements: Dict[str, str]) -> Dict[str, str]:
"""
Replace across multiple files
Args:
files_content: Dictionary of {filename: content}
replacements: Dictionary of {old: new}
Returns:
Dictionary of modified contents
"""
return {filename: replace_multiple(content, replacements)
for filename, content in files_content.items()}
def replace_in_range(text: str, old: str, new: str, start: int, end: int) -> str:
"""
Replace only within range
Args:
text: Input string
old: Substring to replace
new: Replacement string
start: Range start
end: Range end
Returns:
Modified string
"""
before = text[:start]
middle = text[start:end].replace(old, new)
after = text[end:]
return before + middle + after
# Usage Examples
def demonstrate_string_replace():
print("=== Web Python String Replacement Examples ===\n")
# 1. Basic replacement
print("--- 1. Basic Replacement ---")
text = "Hello World, World is beautiful"
print(f"Original: {text}")
print(f"Replace 'World' with 'Earth': {replace_all(text, 'World', 'Earth')}")
print(f"Replace first: {replace_first(text, 'World', 'Earth')}")
print(f"Replace last: {replace_last(text, 'World', 'Earth')}")
print(f"Replace 2nd: {replace_nth(text, 'World', 'Earth', 2)}")
# 2. Count-based
print("\n--- 2. Count-Based Replacement ---")
text = "cat cat cat dog cat"
print(f"Original: {text}")
print(f"Replace 2 cats: {replace_n_times(text, 'cat', 'CAT', 2)}")
print(f"Replace if count > 3: {replace_if_count(text, 'cat', 'CAT', lambda c: c > 3)}")
# 3. Multiple replacements
print("\n--- 3. Multiple Replacements ---")
text = "The quick brown fox jumps over the lazy dog"
replacements = {'quick': 'slow', 'brown': 'white', 'lazy': 'active'}
print(f"Original: {text}")
print(f"After: {replace_multiple(text, replacements)}")
# 4. Pattern-based
print("\n--- 4. Pattern-Based Replacement ---")
text = "Contact: [email protected] or [email protected]"
print(f"Original: {text}")
print(f"Mask emails: {replace_pattern(text, r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}', '***@***.***')}")
# 5. Conditional
print("\n--- 5. Conditional Replacement ---")
text = "Hello World"
print(f"Original: {text}")
print(f"Replace if contains 'World': {replace_if_contains(text, 'World', 'Earth', 'World')}")
# 6. Position-based
print("\n--- 6. Position-Based Replacement ---")
text = "Hello World Python"
print(f"Original: {text}")
print(f"Replace 6-11: {replace_at_position(text, 6, 11, 'Earth')}")
# 7. Template
print("\n--- 7. Template Replacement ---")
template = "Hello {name}, welcome to {place}!"
values = {'name': 'Alice', 'place': 'Wonderland'}
print(f"Template: {template}")
print(f"Filled: {replace_placeholders(template, values)}")
# 8. Smart replacement
print("\n--- 8. Smart Replacement ---")
text = "hello world. this is a test."
print(f"Original: '{text}'")
print(f"Normalize: '{normalize_whitespace(text)}'")
print(f"Capitalize: '{auto_capitalize_after_period(text)}'")
# 9. Advanced
print("\n--- 9. Advanced Replacement ---")
text = "Keep /* remove this */ and keep this"
print(f"Original: {text}")
print(f"Remove between markers: {replace_between(text, '/*', '*/', '')}")
# 10. Word boundaries
print("\n--- 10. Word Boundaries ---")
text = "cat category concatenate"
print(f"Original: {text}")
print(f"Replace 'cat' whole word: {replace_word_boundaries(text, 'cat', 'DOG')}")
print("\n=== All String Replacement Examples Completed ===")
# Export functions
export { replace_all, replace_first, replace_last, replace_nth }
export { replace_n_times, replace_if_count }
export { replace_if_contains, replace_if_length, replace_with_condition }
export { replace_multiple, replace_multiple_sequential, replace_multiple_simultaneous }
export { replace_pattern, replace_pattern_func, replace_case_insensitive, replace_word_boundaries }
export { replace_at_position, replace_at_line, replace_lines_matching }
export { replace_placeholders, replace_placeholders_braced, replace_percent_style }
export { replace_except, replace_between, replace_with_fallback, replace_with_confirmation }
export { auto_capitalize_after_period, fix_double_spaces, fix_double_punctuation }
export { normalize_whitespace, smart_quotes }
export { batch_replace, replace_in_range }
export { demonstrate_string_replace }