简体   繁体   中英

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!

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)
hAndL = hAndL.replace('epsilo\ell','epsilon')
pattern = re.compile("\frac{.*?}{ \frac{.*?}{.*?}}")
matching = pattern.match(hAndL)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM