Imane.r
Imane.r

Reputation: 83

Find occurence of a word in a sentence

I want to find the occurence of a certain word in a sentence and then remove it. I'm able to do it but sometimes the word I wanna remove can be a subword. For example I wanna find/remove the word "word" in the sentence "Music is worldwide". My program will fin/remove return a positive value that it found the word "word" in the sentence while in fact it encountered the word "worldwide" and I would like it to return a negative value. I'm currently using

index = text.find(word)

Is there any other way to avoid this problem of the word being a subword in a sentence ? Thank you in advance!

Upvotes: 1

Views: 171

Answers (2)

Robby Cornelissen
Robby Cornelissen

Reputation: 97351

You can use the regular expression module, and rely on regular expression word boundaries (\b) to only match the complete word.

Since you're trying to remove the word from the sentence, here's an example that replaces all the matches with an empty string:

import re

sentence = 'Music world is worldwide'
word = 'world'
removed = re.sub(r'\b%s\b' % word, '', sentence)

print removed # prints "Music   is worldwide" 

If you just want to find the position of the first occurrence, you can do it as follows:

import re

sentence = 'Music is worldwide in the world'
word = 'world'
match = re.search(r'\b%s\b' % word, sentence)

if match:
    print match.start() # prints 26

Check the documentation of the re module for details.

Upvotes: 2

Imane.r
Imane.r

Reputation: 83

I thought of adding a space to the word i.e :

word = " "+ word
index = text.find(word)

but I wonder if there's a more effective and clean way to do it.

Upvotes: 0

Related Questions