Reputation: 437
I am unable to find explanations to what exactly the flags due for certain operations within the re library.
https://docs.python.org/3.6/library/re.html
for example, I have the following bit of python code:
nestedPar = re.findall(r"\([^\(\)]*\)", s, 0)
but I am unsure what exactly is happening when I change that 0 to a 1, since it results in a crash.
Can anyone explain what this flag means exactly?
Upvotes: 1
Views: 495
Reputation: 25789
The third argument is options bit mask, and bit masks are defined as (this, technically, may vary from version to 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 as multiline string
SRE_FLAG_DOTALL = 16 # treat target as a single string
SRE_FLAG_UNICODE = 32 # use unicode locale
SRE_FLAG_VERBOSE = 64 # ignore whitespace and comments
SRE_FLAG_DEBUG = 128 # debugging
Using a simple bitwise OR (or your regular +
) you can combine multiple flags (e.g. re.MULTILINE | re.DOTALL
). It shouldn't fail on 1
but please don't use numbers directly, they are assigned to meaningful constants for a reason.
EDIT - Template mode (re.T
or re.TEMPLATE
), whose flag is 1
as shown above, is experimental and a lot of things can go wrong with it, directly from the source:
# sre extensions (experimental, don't rely on these)
T = TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE # disable backtracking
So you've encountered a problem with the templating mode. Either way for happy life, do not set the flags yourself :)
Upvotes: 3