Extract letters (and a specific number) from a string

  python, regex, string

I have a list of strings similar to the one below:

l = ['ad2g3f234','4jafg32','fg23g523']

For each string in l, I want to delete every digit (except for 2 and 3 if they appear as 23). So in this case, I want the following outcome:

n = ['adgf23','jafg','fg23g23']

How do I go about this? I tried re.findall like:

w = [re.findall(r'[a-zA-Z]+',t) for t in l]

but it doesn’t give my desired outcome.

Source: Python Questions

LEAVE A COMMENT