Strings#

Python strings ship with most of what the operator needs. Built-in methods, re, and difflib cover scan-line parsing, regex extraction, substring search, and similarity metrics without leaving the standard library.

Built-in methods#

s.split(",")
s.partition("=")
s.startswith(("err", "ERROR"))
s.casefold()                           # better than lower() for caseless cmp
s.translate(str.maketrans("abc", "ABC"))
s.replace("old", "new", 1)             # first occurrence only

Regex#

re is the default. re.compile once if the pattern is reused.

import re

IP = re.compile(r"\b(?:\d{1,3}\.){3}\d{1,3}\b")
for line in lines:
    for ip in IP.findall(line):
        yield ip

m = re.match(r"(?P<host>[^:]+):(?P<port>\d+)", target)
if m:
    host = m["host"]; port = int(m["port"])

For literal-string search at scale, prefer str.find or in over regex.

Distance metrics#

from difflib import SequenceMatcher, get_close_matches

SequenceMatcher(None, "kitten", "sitting").ratio()
get_close_matches("anthropic", ["anthropic", "openai", "google"], n=1)

For Levenshtein at scale, rapidfuzz is the fast third-party option.

References#