Remove text in-between delimiters in a string using a regex?
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
Yes, regular expressions can remove text between delimiters, but the right pattern depends on whether you want the delimiters removed too, whether matches can span multiple lines, and whether nested delimiters are possible. Regex works well for simple non-nested cases; for nested structures, a real parser is often the safer choice.
The Basic Non-Greedy Pattern
Suppose you want to remove text inside square brackets and remove the brackets themselves as well.
The important part is .*?, which is a non-greedy match. Without the question mark, the regex may consume too much.
Why Greedy Matching Often Breaks
Given this input:
A greedy pattern such as \[.*\] matches from the first [ to the last ], which removes more than intended. The non-greedy form \[.*?\] matches the shortest valid span instead.
That is one of the most common regex mistakes in delimiter-based text removal.
Keep or Remove the Delimiters Deliberately
If you want to keep the delimiters and remove only the interior text, use capturing groups.
Now the output keeps the brackets but removes the content between them.
The exact replacement string depends on whether you want empty delimiters, a placeholder, or complete removal.
Multiline Content Needs Special Handling
The dot does not match newline characters by default. If the delimited content can span lines, add the re.DOTALL flag.
Without re.DOTALL, the regex stops at the line boundary and fails to match the full block.
Custom Delimiters Work the Same Way
The delimiters do not have to be brackets. For custom strings such as << and >>, escape only what the regex engine treats as special.
The same non-greedy idea applies.
Regex Is Not Great for Nested Structures
Regex is fine for simple flat delimiters. It is not a good general solution for nested constructs such as:
At that point, a regex-based approach becomes fragile because balanced nested structures are not what ordinary regular expressions handle well.
If nesting matters, write a parser or use a stack-based scan instead.
A Safer Parser for One Delimiter Pair
For nested brackets, a small parser is often clearer than fighting the regex engine.
This is often easier to trust when the input format is more complex than a flat regex pattern can describe.
Common Pitfalls
The most common mistake is using a greedy pattern and removing far more text than intended.
Another common issue is forgetting re.DOTALL when the delimited text spans multiple lines. Developers also often reach for regex even when the input can contain nested delimiters, where a parser is the safer tool.
Summary
- Use a non-greedy regex such as
\[.*?\]for simple flat delimiter removal. - Decide explicitly whether the delimiters themselves should stay or disappear.
- Add
re.DOTALLwhen matches may span multiple lines. - Regex is appropriate for simple non-nested structures.
- Use a parser or stack-based scan when delimiters can be nested.
Related reading
- Remove the last three characters from a string
- Remove trailing delimiting character from a delimited string
- Removing a list of characters in string
- Removing the first 3 characters from a string
- Replace carets with HTML superscript markup using Java
- Replace Line Breaks in a String C
- Replace non-ASCII characters with a single space
- Replacing all non-alphanumeric characters with empty strings
.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.