Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Regex-- TypeError: an integer is required

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!

like image 242
zara Avatar asked Jun 15 '15 16:06

zara


1 Answers

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)
like image 78
jonrsharpe Avatar answered Oct 29 '22 11:10

jonrsharpe