removing a character from beginning or end of a string in Python

python, regex

Solution

import re
out = re.sub(r'^\s*(-\s*)?|(\s*-)?\s*$', '', input)

This will remove at most one instance of `-` at the beginning of the string and at most one instance of `-` at the end of the string. For example, given input `- - text - - `, the output will be `- text -`.

Note that `\s` matches Unicode whitespaces (in Python 3). You will need `re.ASCII` flag to revert it to matching only `[ \t\n\r\f\v]`.

Since you are not very clear about cases such as ` -text`, `-text-`, `-text -`, the regex above will just output `text` for those 3 cases.

For strings such as ` text `, the regex will just strip the spaces.

Problem

I have a string that for example can have `-` any where including some white spaces. I want using regex in Python to remove `-` only if it is before all other non-whitespace chacacters or after all non-white space characters. Also want to remove all whitespaces at the beginning or the end. For example: ``` string = ' - test ' ``` it should return ``` string = 'test' ``` or: ``` string = ' -this - ' ``` it should return ``` string = 'this' ``` or: ``` string = ' -this-is-nice - ' ``` it should return ``` string = 'this-is-nice' ```

Original source