Algorithmssubstring extraction logic

Substring Extraction (partition, delimiters)

TT
Testlaa Team
May 14, 20261 min read

Extraction pulls substrings between markers—split, partition, regex groups, or manual scans for delimiters like quotes or XML tags. Always define what happens when a delimiter is missing.

Why this shows up in the real world

Log field extractors grab text between brackets. CSV with quotes needs stateful parsing, not naive split.

Core idea (explained for students)

before, sep, after = s.partition('>') gives safe pieces even if sep missing (sep empty). For repeated blocks, loop while with moving index.

Try this in Python

def between_first_dash(s: str) -> str:
    a, dash, b = s.partition("-")
    return b if dash else ""


print(between_first_dash("key-value-extra"))

Common mistakes

  • Greedy regex .* eating too much—use non-greedy *? or explicit delimiters.
  • Zero-width matches confusing empty extracts.

Key takeaways

  • Prefer partition/split first; escalate to regex when structure is irregular.
  • Unit-test missing closing delimiter cases.

Tags:

StringsPythonStudents