Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/imcflibs/strtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,33 @@ def pad_number(index, pad_length=2):
'0042'
"""
return str(index).zfill(pad_length)


def text_in_last_bracket_group(text):
"""Return the content of the last balanced square-bracket group.

Parameters
----------
text : str
The input string to search for the last balanced square-bracket group.

Returns
-------
str or None
The content of the last balanced square-bracket group, or None if no such
group exists
"""
depth = 0
end = None

for index in range(len(text) - 1, -1, -1):
if text[index] == "]":
if end is None:
end = index
depth += 1
elif text[index] == "[":
depth -= 1
if depth == 0:
return text[index + 1 : end]

return None
Loading