How to use a variable inside a regular expression?

后端 未结 10 1872
青春惊慌失措
青春惊慌失措 2020-11-22 03:29

I\'d like to use a variable inside a regex, how can I do this in Python?

TEXTO = sys.argv[1]

if re.search(r\"\\b(?=\\         


        
10条回答
  •  醉话见心
    2020-11-22 03:51

    if re.search(r"\b(?<=\w)%s\b(?!\w)" % TEXTO, subject, re.IGNORECASE):
    

    This will insert what is in TEXTO into the regex as a string.

提交回复
热议问题