Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove non-letter characters from beginning and end of a string

Tags:

python

regex

I need to remove all non-letter characters from the beginning and from the end of a word, but keep them if they appear between two letters.

For example:

'123foo456' --> 'foo'
'2foo1c#BAR' --> 'foo1c#BAR'

I tried using re.sub(), but I couldn't write the regex.

like image 487
iomartin Avatar asked Oct 12 '12 16:10

iomartin


2 Answers

like this?

re.sub('^[^a-zA-Z]*|[^a-zA-Z]*$','',s)

s is the input string.

like image 92
Kent Avatar answered Nov 14 '22 22:11

Kent


You could use str.strip for this:

In [1]: import string

In [4]: '123foo456'.strip(string.digits)
Out[4]: 'foo'

In [5]: '2foo1c#BAR'.strip(string.digits)
Out[5]: 'foo1c#BAR'

As Matt points out in the comments (thanks, Matt), this removes digits only. To remove any non-letter character,

Define what you mean by a non-letter:

In [22]: allchars = string.maketrans('', '')

In [23]: nonletter = allchars.translate(allchars, string.letters)

and then strip:

In [18]: '2foo1c#BAR'.strip(nonletter)
Out[18]: 'foo1c#BAR'
like image 35
unutbu Avatar answered Nov 14 '22 23:11

unutbu