-1

I have the following phrase:

phrase = 'My love for ict is strict'

I want to find specific phrase from a list.

phrase_list = ['My love', 'ict']

How can i find the exact phrases in phrase list in the phrase string?

I tried:

for p in phrase_list:
   pattern = re.compile(p)  
   result = pattern.findall(phrase)
   print(result)

The problem is that this prints:

['My love']
['ict','ict']

I would only want the occurrence of 'ict' that is an exact match:

['My love']
['ict']

How can I accomplish this for a large number of phrases?

Felix
  • 1,837
  • 9
  • 26
Atma
  • 29,141
  • 56
  • 198
  • 299

1 Answers1

0

You can use \b to match a word break (the boundary between a word and something that is not a word, such as punctuation or a space). Try changing ict to \bict\b

TallChuck
  • 1,725
  • 11
  • 28