Passing in flags as an argument to re.compile

前端 未结 1 1203
甜味超标
甜味超标 2021-01-02 03:08

I would like to pass certain flags into a re.compile function based on logic similar to the below. I was wondering whether it\'s possible to do so.

  flags =         


        
相关标签:
1条回答
  • 2021-01-02 03:51

    re flags are just numbers. So, we need to binary OR them, like this

    flags = 0
    
    if multiline:
      flags = re.M
    
    if dotall:
      flags |= re.S
    
    if verbose:
      flags |= re.X
    
    if ignorecase:
      flags |= re.I
    
    if uni_code:
      flags |= re.U
    
    regex = re.compile(r'Test Pattern', flags)
    
    0 讨论(0)
提交回复
热议问题