Python Regex-- TypeError: an integer is required

2.1k Views Asked by At

I am receiving an error on the .match regex module function of TypeError: An integer is required.

Here is my code:

hAndL = hAndL.replace('epsilo\ell','epsilon')
pattern = re.compile("\frac{.*?}{ \frac{.*?}{.*?}}")
matching = pattern.match(pattern,hAndL)

hAndL is a string and pattern is a..pattern.

I'm not sure why this error is happening, any help is appreciated!

2

There are 2 best solutions below

0
On BEST ANSWER

When you re.compile a regular expression, you don't need to pass the regex object back to itself. Per the documentation:

The sequence

prog = re.compile(pattern)
result = prog.match(string)

is equivalent to

result = re.match(pattern, string)

The pattern is already provided, so in your example:

pattern.match(pattern, hAndL)

is equivalent to:

re.match(pattern, pattern, hAndL)
                # ^ passing pattern twice
                         # ^ hAndL becomes third parameter

where the third parameter to re.match is flags, which must be an integer. Instead, you should do:

pattern.match(hAndL)
0
On
hAndL = hAndL.replace('epsilo\ell','epsilon')
pattern = re.compile("\frac{.*?}{ \frac{.*?}{.*?}}")
matching = pattern.match(hAndL)