Split string using regex

Feb. 15, 2022

Posted in:

Sometime you need to split a string using a delimiter but you want to excludes cases where the delimiter appears more than once. For example, you want to split the following string by the delimiter "|" (without quotes) in the string:

input_string = "foo|bar|baz||baz|bar|foo"

However, you want the result to be:

>>> ["foo", "bar", "baz||baz", "bar", "foo"]

This is where look ahead and look behind regex feature helps.

The regular expression below uses look behind negative and look ahead negative to differentiate between | and || when splitting theĀ  string into parts.

re.split(r"(?<!\|)\|(?!\|)", input_string)
>>> ['foo', 'bar', 'baz||baz', 'bar', 'foo']


Return to blog