简体   繁体   中英

Remove digits from a string and all connecting characters

I've seen many solutions to the first part of my question, such as using:

name = 'Example 01 string 1a%'
new_name = ''.join(i for i in name if not i.isdigit())
print(new_name)
>> 'Example  string a%'

However I would like the output to also replace 2+ adjacent spaces with only one space, and any characters that were connected to the removed digits (excluding spaces), so the result I expect in this example would be:

>> 'Example string'

I know that to remove for example double spaces, you could simple use:

new_name.replace('  ', ' ')

However this only works for double spaces. I am not sure how to extend this to an arbitrary amount, in case the string has multiple sets of digits which would be removed and leave behind large groups of space.

So my question is, how do I clean up a string to remove digits and any characters (excluding spaces) connected to those digits, and then replace any large groups of spaces with a single one?

One way to accomplish this is using regex, via the built-in re library.

Essentially, the exp extracts two or more adjacent letters and returns the results into a list, which is converted to a string using the join function, as you are currently using.

The caveat to this exact method, is that the search string must be at least two alpha characters in length. However, regex features look-arounds if you'd like to enhance the expression.

import re

name = 'Example 01 string 1a%'
exp = '([a-zA-Z]{2,})'

' '.join(re.findall(exp, name))

Output:

>>> 'Example string'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM