Skip to content

Instantly share code, notes, and snippets.

@jacobvalenta
Last active February 21, 2023 19:36
Show Gist options
  • Save jacobvalenta/ec3ec243348c247d5dc5bef16a30ba40 to your computer and use it in GitHub Desktop.
Save jacobvalenta/ec3ec243348c247d5dc5bef16a30ba40 to your computer and use it in GitHub Desktop.

Python get text between brackets

Sample Text

This text is [split up] into sections. the [deliminating character] is the [square bracket].
[[groups may be nested] within [one another]].

Code

def get_phrases(text):
    phrase_indexes = []
    phrases = []

    for i, _char in enumerate(text):
        if _char == "[":
            phrase_indexes.append([i, None])
        elif _char == "]":
            for j, backtracked_phrase in sorted(enumerate(phrase_indexes), reverse=True):
                if backtracked_phrase[1] == None:
                    phrase_indexes[j][1] = i + 1
                    break

    for phrase in phrase_indexes:
        phrases.append(text[phrase[0]:phrase[1]] \
            .replace('[', '').replace(']', ''))

    return phrases

Use

>>> get_phrases(sample_text)
['split up', 'deliminating character', 'square bracket', 'groups may be nested within one another', 'groups may be nested', 'one another']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment