0

I am trying to detect an exact word match in a string by iterating over a set of keywords. Therefore, I wanted to dynamically build a regex pattern that includes the keyword found in the loop. Like this:

for i, entry in sentiLexicon.iterrows():
    word = entry.word
    if re.match('\b' + word + '\b', text):
        print(word)

I included the '\b' since I only want exact words to match, for example, the word 'stand' should not match with the word 'abstand'. I am getting the following error but it doesn't make any sense to me since I did not include a parenthesis anywhere.

re.error: unbalanced parenthesis at position 3

Does anyone have an idea on how to implement this correctly?

  • All your issues are well-known and have been discussed on SO. 1) Escape the literal pattern parts using `re.escape()`, 2) `\b` is a word boundary, but you defined a backspace char (use `r'\b'`, not `'\b'`). Also, you might need `(?<!\w)` and `(?!\w)`, not `\b`. – Wiktor Stribiżew Nov 14 '20 at 15:26
  • Maybe [this answer of mine](https://stackoverflow.com/questions/29996079/match-a-whole-word-in-a-string-using-dynamic-regex) will explain it all in a better way. And one more hint: use `re.search` to search for matches anywhere inside a string, `re.match` only searches at the string start. – Wiktor Stribiżew Nov 14 '20 at 15:29

0 Answers0