Inherited Geek Inherited Geek - 4 years ago 73
Python Question

Python RegEx to replace string

How do I use RegEx (or something else in Python) for the following requirement?
I need to:

  1. Remove the word "dream" (including all its stems)

  2. Remove All previous words (i.e. all words behind the word "dream")

  3. Remove the Word next to it (in front of it/to the right of "dream")

  4. Remove the word "to" from all phrases.


text = ["Dream of a car",
"Dream to live in a world",
"Dream about 8am every morning",
"stopped dreaming today",
"still dreaming of a car",
"One more dream to come late tomorrow",
"Dream coming to hope tomorrow"]

Required Output:

["a car",
"live in a world",
"8am every morning",
" ",
"a car",
"come late tomorrow",
"hope tomorrow"]

I tried:

result = [re.sub('Dream', '', a) for a in text]

# MyOutput
[' of a car', ' to live in a world', ' about 8am every morning', 'stopped dreaming today', 'still dreaming of a car', 'One more dream to come late tomorrow', ' coming to hope tomorrow']

Answer Source

This gives your required output

result = [re.sub(r'\bto\b *', '', re.sub(r'^.*Dream[^ ]* *[^ ]* *', '', a, flags=re.I)) for a in text]

If you only want to remove the to at the front

result = [re.sub(r'^.*Dream[^ ]* *[^ ]* *(\bto\b)? *', '', a, flags=re.I) for a in text]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download