-1

I am trying to extract words from item_search list. My regular expression works for normal words. However, it does not work for the words that contain unbalanced parenthesis. Here is the code:

import re
strn = """
Hello every@one, I am trying@this code/module-but not work(ing properly)for 
unknown reason(s). It always stop with @regula-expression_error12
"""
item_search = ["Hello", "every@one","trying@this", "code/module-but", "work(ing properly)", "reason(s)", "@regula-expression_error12)"  ]
item=list(set(re.findall(rf"(?<!\S)({'|'.join(item_search)})\W*(?!\S)", strn, flags=re.MULTILINE | re.UNICODE)))
item

error:

error: unbalanced parenthesis at position 108

Looking for experts suggestions.

1 Answers1

0

Look at the documentation for re.escape. You are dynamically building a regular expression, but one of your components contains a character that is special to regexp. You must quote that character for it to be used in a regular expression.

Frank Yellin
  • 9,127
  • 1
  • 12
  • 22
  • Thanks for your answer. I tried it but seems not working: [link](https://stackoverflow.com/questions/280435/escaping-regex-string). Would you please elaborate it with code. – Syful Islam Dec 23 '20 at 01:30
  • 1
    Try replacing `item_search` with `re.escape(item_search)`. – Frank Yellin Dec 23 '20 at 01:35