regex - What exactly do the flags do in the re library for python 3? -
i unable find explanations flags due operations within re library.
https://docs.python.org/3.6/library/re.html
for example, have following bit of python code:
nestedpar = re.findall(r"\([^\(\)]*\)", s, 0)
but unsure happening when change 0 1, since results in crash.
can explain flag means exactly?
the third argument options bit mask, , bit masks defined (this, technically, may vary version version):
sre_flag_template = 1 # template mode (disable backtracking) sre_flag_ignorecase = 2 # case insensitive sre_flag_locale = 4 # honour system locale sre_flag_multiline = 8 # treat target multiline string sre_flag_dotall = 16 # treat target single string sre_flag_unicode = 32 # use unicode locale sre_flag_verbose = 64 # ignore whitespace , comments sre_flag_debug = 128 # debugging
using simple bitwise or (or regular +
) can combine multiple flags (e.g. re.multiline | re.dotall
). shouldn't fail on 1
please don't use numbers directly, assigned meaningful constants reason.
edit - template mode (re.t
or re.template
), flag 1
shown above, experimental , lot of things can go wrong it, directly source:
# sre extensions (experimental, don't rely on these) t = template = sre_compile.sre_flag_template # disable backtracking
so you've encountered problem templating mode. either way happy life, not set flags :)
Comments
Post a Comment