-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?

3
  • do you mean you don't want to match the ict in strict ? Commented Oct 19, 2018 at 21:48
  • @TallChuck yes exactly! Commented Oct 19, 2018 at 21:51
  • Duplicate Commented Oct 19, 2018 at 21:52

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.