Fix Python ValueError: cannot process flags argument with a compiled pattern

By | October 7, 2021

When we are using re.findall() function, we may get this error: ValueError: cannot process flags argument with a compiled pattern. In this tutorial, we will introduce you how to fix it.

Look at this example:

def getStart(ctx, tag):
    pattern = re.compile(r''+tag+'[0-9 ]{1,}') 
    matcher = re.findall(pattern, ctx, re.I)
    lx = len(matcher)
    sx = []
    if  lx > 3:
        return lx
    return 0

text = 'dfafstep 1dfa, Step 2,adga step 4, ada step 4 adf step 6'
d = getStart(text, 'step')
print(d)

Run this code, you will see:

Fix Python ValueError: cannot process flags argument with a compiled pattern

How to fix this value error?

We should use flag argument in re.compile() not in re.findall().

For example:

def getStart(ctx, tag):
    pattern = re.compile(r''+tag+'[0-9 ]{1,}', re.I) 
    matcher = re.findall(pattern, ctx)
    lx = len(matcher)
    sx = []
    if  lx > 3:
        return lx
    return 0

Then you will find this error is fixed.

Leave a Reply