denpy
denpy

Reputation: 279

Extract full sentence with list of words

I hope to extract the full sentence, if containing certain key words (like or love).

text = 'I like blueberry icecream. He has a green car. She has blue car.'
pattern = '[^.]* like|love [^.]*\.'
re.findall(pattern,text)

Using | for the divider , I was expected ['I like blueberry icecream.']

But only got ['I like']

I also tried pattern = '[^.]*(like|love)[^.]*\.' but got only ['like']

What did I do wrong as I know single word works with following RegEx - '[^.]* like [^.]*\.'

Upvotes: 1

Views: 624

Answers (4)

osint_alex
osint_alex

Reputation: 1022

I actually think it would be easier to do this without regex. Just my two cents.

text = 'I like blueberry icecream. He has a green car. She has blue car. I love dogs.'
print([x for x in text.split('.') if any(y in x for y in ['like', 'love'])])

Upvotes: 1

mk23
mk23

Reputation: 1403

You can use below regex

regex = /[^.]* (?:like|love) [^.]*\./g

Demo here

Upvotes: 0

Barmar
Barmar

Reputation: 782488

You need to put a group around like|love. Otherwise the | applies to the entire patterns on either side of it. So it's matching either a string ending with like or a string beginning with love.

pattern = '[^.]* (?:like|love) [^.]*\.'

Upvotes: 3

denpy
denpy

Reputation: 279

Research more and found out I was missing ?:

text = 'I like blueberry icecream. He has a green car. She has blue car.'
pattern = '[^.]*(?:like|love)[^.]*\.'

Output

['I like blueberry icecream.']

Source: https://www.ocpsoft.org/tutorials/regular-expressions/or-in-regex/

Upvotes: 2

Related Questions