Search code examples
pythonstringstrip

Python: what's a more compact/efficient way to strip string of a suffix from list?


Is there a more compact and efficient way of stripping a string of any suffix from a given list, i.e.:

sfxs = ['suffix1', 'sfx2', 'suffix333']
s = 'string-to-process-sfx2'
for sfx in sfxs:
    i = s.find(sfx)
    if not i == -1:
        s = s[:i]
        break

Suffixes are of different lengths


Solution

  • You may use re.sub.

    >>> import re
    >>> sfxs = ['suffix1', 'sfx2', 'suffix333']
    >>> s = 'string-to-process-sfx2'
    >>> re.sub(r'(' + '|'.join(sfxs) + r')$', '',s)
    'string-to-process-'
    >>> re.sub(r'\b(' + '|'.join(sfxs) + r')$', '',s)
    'string-to-process-'
    
    >>> re.sub(r'-(' + '|'.join(sfxs) + r')$', '',s)
    'string-to-process'
    

    '|'.join(sfxs) helps to join the suffix list with | as delimiter. So r'(' + '|'.join(sfxs) + r')$' would form a regex like (suff1|suff2|suff3|..)$. Note that $ anchor, which matches the end of the line. So this would do matching only at the end.

    >>> re.sub(r'(' + '|'.join(sorted(sfxs, key=lambda x:len(x), reverse=True)) + r')$', '',s)
    'string-to-process-'