Extract measurement dimensions and number from string using regex

  nlp, python, regex
import re

punctuation = '!"#$%&'*()+,-:;<=>[email protected][]^_{|}~`'

train_new['priceDescription'] = '''His speech talked  L36MM of the setbacks in 35CMx56cm life, including death, and L458905 how being aware of death 35CM can 56x34 help you make better 35MM choices in life. At 69cm the time, Jobs was dying 34/67 of pancreatic cancer, and 23 his inspirational words 2.3x50cm 3.9MM on the importance of acquiring 475MMx3.9cm knowledge and following your dreams was the best life lesson he could bestow upon the graduates'''

def remove_punctuation(text):
    text = re.sub("[^0-9MMXCML.]", " ", text)
    text = re.sub( r".*(MM).*",r"", text )
    text = text.lower()
    no_punct=[words for words in text if words not in punctuation]
    words_wo_punct=''.join(no_punct)
    return words_wo_punct
train_new['priceDescription']=train_new['priceDescription'].apply(lambda x: remove_punctuation(x))
train_new['priceDescription'].apply(lambda x: len(x.split(' '))).sum()
print(train_new)

I just want to extract dimension and numbers from above string like 35CMx56cm, L458905,L36MM, 23, 475MMx3.9cm, 34MM, 25CM

Source: Python Questions

LEAVE A COMMENT