I want to extract part of a string in a list which does not have a space followed by number in python.
# INPUT
text = ['bits', 'scrap 1.2', 'bits and pieces', 'junk 3.4.2']
# EXPECTED OUTPUT
output = ['bits', 'scrap', 'bits and pieces', 'junk']
I managed to do this using re.sub or re.split:
output = [re.sub(" [0-9].*", "", t) for t in text]
# OR
output = [re.split(' \d',t)[0] for t in text]
When I tried to use re.search and re.findall, it return me empty list or empty result.
[re.search('(.*) \d', t) for t in text]
#[None, <_sre.SRE_Match object; span=(0, 7), match='scrap 1'>, None, <_sre.SRE_Match object; span=(0, 6), match='junk 3'>]
[re.findall('(.*?) \d', t) for t in text]
#[[], ['scrap'], [], ['junk']]
Can anyone help me with the regex that can return expected output for re.search and re.findall?