string manipulation
split string
multiple delimiters
text processing
programming tutorial

splitting a string based on multiple char delimiters

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you need to split a string on more than one delimiter, the right tool is usually a regular expression. The important distinction is whether your delimiters are single characters such as commas and semicolons, or multi-character tokens such as -- and ||, because the regex pattern should reflect that difference.

Use Regex Alternation for Multiple Delimiters

In Python, re.split is the standard answer.

python
1import re
2
3text = "apple--banana||cherry"
4parts = re.split(r"--|\|\|", text)
5print(parts)

This returns:

text
['apple', 'banana', 'cherry']

The pattern uses alternation, meaning “split on delimiter A or delimiter B.” This is the right model when the delimiters are multi-character strings.

Use a Character Class Only for Single-Character Delimiters

If your delimiters are single characters, a character class is shorter.

python
1import re
2
3text = "apple,banana;cherry orange"
4parts = re.split(r"[,; ]+", text)
5print(parts)

Here, the pattern means “one or more of comma, semicolon, or space.”

That is different from multi-character token splitting. A character class cannot represent whole delimiter strings such as -- as a single unit.

Keep Empty Fields in Mind

Splitting can produce empty strings when delimiters appear next to each other or at the ends.

python
1import re
2
3text = "apple--||banana--"
4parts = re.split(r"--|\|\|", text)
5print(parts)

Depending on the input, you may need to filter empty pieces.

python
parts = [part for part in re.split(r"--|\|\|", text) if part]

Whether that is correct depends on whether empty fields are meaningful in your data format.

Escape Delimiters Carefully

Some delimiters contain regex metacharacters. For example, |, ., ?, +, and * all have special meaning in regex patterns.

That is why || in a delimiter pattern must be escaped as \|\| rather than written literally in the regex.

If the delimiters come from outside input, re.escape is often safer than writing the pattern manually.

Use Plain split Only When the Problem Is Simple

If you have only one delimiter, built-in split is simpler.

python
text = "apple--banana--cherry"
parts = text.split("--")

But once you need several delimiters, re.split is usually clearer than chaining multiple replacements or nested splits.

Common Pitfalls

  • Using a character class when the delimiters are actually multi-character tokens.
  • Forgetting to escape regex metacharacters such as | or ..
  • Accidentally dropping meaningful empty fields after splitting.
  • Writing a regex that matches too broadly and cuts the string in the wrong places.
  • Using complex regex when a single plain split would have solved the actual problem.

Summary

  • Use re.split when you need to split on several delimiters.
  • Use alternation for multi-character delimiters such as --|\|\|.
  • Use character classes only for sets of single-character delimiters.
  • Decide explicitly whether empty pieces should be kept or filtered out.
  • Escape delimiter text correctly when it contains regex-special characters.

Course illustration
Course illustration

All Rights Reserved.