Home > Blockchain >  Python regex error: re.error: missing : at position 40
Python regex error: re.error: missing : at position 40

Time:04-10

When running the following code, ran into this error:

re.error: missing : at position 40

import re
re_keywords = ["Bitcoin", "Ethereum", "Tether", r"(?-i)BNB\s Coin"]
re_keywords = [r"("   kw   r")" for kw in re_keywords]
re_keywords = "|".join(re_keywords)
re_keywords = r"\b(?:"   re_keywords   r")\b"
print(re_keywords)
re_keywords = re.compile(re_keywords, re.I)

I'm using Python 3.8. What was wrong? Thanks a lot.

CodePudding user response:

Python does not support your inline flag in this manner. re requires:

  • The flag be at the start
  • The flag is not as part of a non-capturing group modifier

See here

A valid form would be:

(?i)\\b(?:(Bitcoin)|(Ethereum)|(Tether)|(BNB\\s Coin))\\b

But the flag is changed from (?-i) to (?i), and is moved to the start

  • Related