-2

I have a list of strings and I am trying to run a regex search on it.

I use:

re.search(r"\b" + word + r"\b", list[item], re.I)

Yet, the result of the search doesn't take into account the word boundaries and still returns wrong results. Example:

when word is try and i run the test, it still returns True for trying or country.

What am I doing wrong?

Nicolas
  • 6,611
  • 3
  • 29
  • 73
Ilias
  • 5
  • 2

1 Answers1

0

As mentioned in comments, your pattern looks fine.

Minor quip though--if you have a list of strings, you can simply do this instead:

for string in string_list:
    result = re.search(r"\b" + word + r"\b", string, re.I)

It avoids using the built-in Python function list.

moogle
  • 110
  • 9