Home > other >  Extract letters (and a specific number) from a string
Extract letters (and a specific number) from a string

Time:10-25

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.

CodePudding user response:

One way would be just to replace the string twice:

[re.sub("\d", "", i.replace("23", "*")).replace("*", "23") for i in l]

Output:

['adgf23', 'jafg', 'fg23g23']

CodePudding user response:

Use a placeholder with re.sub

l = ['ad2g3f234','4jafg32','fg23g523']
w = [re.sub('#','23',re.sub('\d','',re.sub('23','#',t))) for t in l]

['adgf23', 'jafg', 'fg23g23'] 

EDIT

As answered by Chris, the approach is the same although string replace will be a better alternative stack_comparison

CodePudding user response:

Using re.sub with function

import re

def replace(m):
    if m.group() == '23':
        return m.group()
    else:
        return ''
    
l = ['ad2g3f234','4jafg32','fg23g523']
w = [re.sub(r'23|\d', replace, x) for x in l]

#w: ['adgf23', 'jafg', 'fg23g23']

Explanation

re.sub(r'23|\d', replace, x)
- checks first for 23, next for a digit
- replace function leaves alone match with 23
- changes match with digit to null string.
  • Related