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

like this?

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

s is the input string.


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'

With your two examples, I was able to create a regex using Python's non-greedy syntax as described here. I broke up the input into three parts: non-letters, exclusively letters, then non-letters until the end. Here's a test run:

1:[123]   2:[foo]   3:[456]
1:[2]   2:[foo1c#BAR]   3:[]

Here's the regular expression:

^([^A-Za-z]*)(.*?)([^A-Za-z]*)$

And mo.group(2) what you want, where mo is the MatchObject.

Tags:

Python

Regex