String Processing and Regex

May 30, 2026 | 5 min read

Manual Parsing

def parse_csv_line(line: str) -> list[str]:
    fields = []
    field = []
    in_quotes = False
    for ch in line:
        if ch == '"':
            in_quotes = not in_quotes
        elif ch == ',' and not in_quotes:
            fields.append(''.join(field))
            field = []
        else:
            field.append(ch)
    fields.append(''.join(field))
    return fields

String Building

def build_report(lines: list[str]) -> str:
    parts = []
    for line in lines:
        parts.append(line.upper())
        parts.append('\n')
    return ''.join(parts)

Regex vs Pure Python

The re module is C-backed and very fast. Use it for complex patterns. Use Pyvorin-compiled pure Python for simple character-by-character processing.