Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

regex

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'
like image 922
TJ1 Avatar asked Dec 16 '22 02:12

TJ1


2 Answers

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'
like image 117
Ashwini Chaudhary Avatar answered May 18 '23 12:05

Ashwini Chaudhary


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.

like image 33
nhahtdh Avatar answered May 18 '23 12:05

nhahtdh