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'
                You don't need regex for this. str.strip strip removes all combinations of characters passed to it, so pass ' -' or '- ' to it.
>>> s = '  -  test '
>>> s.strip('- ')
'test'
>>> s = '  -this - '
>>> s.strip('- ')
'this'
>>> s =  '  -this-is-nice - '
>>> s.strip('- ')
'this-is-nice'
To remove any type of white-space character and '-' use string.whitespace + '-'.
>>> from string import whitespace
>>> s =  '\t\r\n  -this-is-nice - \n'
>>> s.strip(whitespace+'-')
'this-is-nice'
                        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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With