0

I have a text string like following. I want to remove all small words from text till the length of 2 alphabets except a few words

st = "PD never failed to be, it is very good"

To remove small words till the length of 2 I am using below regex, which is working fine

re.sub(r'\b\w{1,2}\b',' ', str(st))

But I don't want PD to be removed, as below list words I don't want to delete.

list = ['PD', 'CD', 'LD']

How can we do this with regex

desire result :- "PD never failed  ,     very good"
Manu Sharma
  • 1,593
  • 4
  • 25
  • 48

0 Answers0