Add remove_enclosed function

This commit is contained in:
tcely 2025-03-21 14:37:09 -04:00 committed by GitHub
parent e2f36d8e85
commit ebea5d8369
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -201,3 +201,24 @@ def profile_func(func):
return (result, (s.getvalue(), ps, s,),)
return wrapper
def remove_enclosed(haystack, /, open='[', close=']', sep=' ', *, valid=None, start=None, end=None):
if not haystack:
return haystack
assert open and close, 'open and close are required to be non-empty strings'
o = haystack.find(open, start, end)
sep = sep or ''
n = close + sep
c = haystack.find(n, len(open)+o, end)
if -1 in {o, c}:
return haystack
if valid is not None:
content = haystack[len(open)+o:c]
found = set(content)
valid = set(valid)
invalid = found - valid
# assert not invalid, f'Invalid characters {invalid} found in: {content}'
if invalid:
return haystack
return haystack[:o] + haystack[len(n)+c:]