How to get a string after a specific substring?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Extracting text after a specific substring is a common parsing task in logs, URLs, and config strings. The implementation looks simple, but edge cases like missing delimiter and repeated delimiter often cause bugs. A robust approach should define expected behavior clearly before writing code.
Python Approaches That Are Safe and Readable
Python offers several clean methods for this task. partition is often the safest because it always returns a three part tuple.
If delimiter is absent, sep is empty so you can branch safely.
Another option is split with max split count:
Use max split equal to one when you want content after first occurrence.
Extract After Last Occurrence
Sometimes you need content after the last occurrence of delimiter, such as filename after final slash.
rpartition is ideal for this case because it scans from right side and keeps logic explicit.
Regular Expressions for Pattern Based Delimiters
If delimiter logic depends on pattern rather than fixed text, regex can help.
Regex is powerful but should be used only when fixed substring methods are insufficient.
API Design for Reuse
Wrap extraction logic in a utility function with explicit defaults. This avoids repeated ad hoc code across services.
Unit tests should cover missing marker, empty marker, and multiple marker appearances.
Cross Language Patterns and Performance Notes
The same extraction idea appears in many languages. Knowing equivalent methods helps when you maintain mixed stacks.
JavaScript example:
C# example:
For large log files, avoid repeatedly scanning long strings when marker position can be cached or when parser state can be streamed line by line. Also normalize encoding and whitespace early so substring extraction behavior is consistent across input sources.
Build Tests for Ambiguous Input
Extraction bugs usually appear with ambiguous content. Include tests for repeated markers, delimiter at end of string, and Unicode text.
Common Pitfalls
- Assuming delimiter always exists and indexing split result directly.
- Using full regex parsing for simple fixed delimiters.
- Forgetting to specify first or last occurrence requirements.
- Ignoring whitespace normalization after extraction.
- Not handling empty marker input in utility functions.
Summary
- Use
partitionorsplitfor fixed delimiter extraction. - Use
rpartitionwhen last occurrence logic is required. - Add defaults for missing delimiter cases to avoid crashes.
- Reserve regex for pattern based extraction needs.
- Centralize helper logic and test edge cases thoroughly.
- Document delimiter assumptions in parser code so future maintainers preserve expected extraction semantics when input formats evolve.
Related reading
- How to get all hugging face models list using python?
- How to get bag of words from textual data?
- How to Get Dependency Parse Output from SyntaxNet
- How to get last 4 characters of a string?
- How to grep a yaml value
- How to increase weight of a word for CountVectorizer
- How to initialize word-embeddings for Out of Vocabulary Word?
- How to load the saved tokenizer from pretrained model
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.