Validating python regex flags specified in json -


i writing tool accepts user configuration via json file. 1 piece of config python regular expression , optional regex flags. configuration regex flags array of integers run through bitwise or (|) , sent re compile method.

my question how can validate these integers ensure valid re flags?

edit: or potentially solution problem... possible user specify actual re flags in json? i.e., [re.debug, re.ignorecase] etc etc , somehow translate json file in python script?

you can define dictionary of possible flags (they few, see re 6.2.2. module contents), , value corresponding key.

a python demo:

import re re_flags = { 're.a' : re.a,      're.ascii' : re.ascii,     're.debug' : re.debug,     're.i' : re.i,     're.ignorecase' : re.ignorecase,     're.l' : re.l,     're.locale' : re.locale,     're.m' : re.m,     're.multiline' : re.multiline,     're.s' : re.s,     're.dotall' : re.dotall,     're.x' : re.x,     're.verbose' : re.verbose } flg = 're.i'                      # user input if flg in re_flags:               # if dict contains key     print(re_flags[flg])          # print value (re.i = 2) 

if still want go numbers instead:

import re print(re.a)           # 256 print(re.ascii)       # 256 print(re.debug)       # 128 print(re.i)           # 2 print(re.ignorecase)  # 2 print(re.l)           # 4 print(re.locale)      # 4 print(re.m)           # 8 print(re.multiline)   # 8 print(re.s)           # 16 print(re.dotall)      # 16 print(re.x)           # 64 print(re.verbose)     # 64 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -