Add word boundary syntax to string list

Please indicate me a message if it already exists for this question.

How can I effectively add word boundary syntax to a list of strings?

So, for example, I want to make sure that the words below in badpositionscorrespond only to the word as a whole, so I would like to use re.search('\bword\b', text).

How to get words in poor positions, to take shape ['\bPresident\b', '\bProvost\b'], etc.

text = ['said Duke University President Richard H. Brodhead. "Our faculty look forward']
badpositions = ['President', 'Provost', 'University President', 'Senior Vice President'] 
+3
source share
1 answer
re_badpositions = [r"\b{word}\b".format(word=word) for word in badpositions]

indexes = {badpositions[i]:re.search(re_badpositions[i],text) for i in range(len(badpositions))}

If you understand correctly, you are looking for the source index of all words that match exactly (i.e. \bWORD\b) in your string text. This is how I did it, but I definitely add a step here, you can just as easily:

indexes = {word: re.search("\b{word}\b".format(word=word),text) for word in badpositions}

, , . - .

+6

All Articles