Python Regex-- TypeError: an integer is required

Python Regex-- TypeError: an integer is required

我收到有关 TypeError 的 .match 正则表达式模块函数的错误:需要一个整数。

这是我的代码:

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

hAndL 是一个字符串,pattern 是一个..pattern。

我不确定为什么会出现此错误,如有任何帮助,我们将不胜感激!

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

当您 re.compile 一个正则表达式时,您 不需要将正则表达式对象传回给它自己 。根据文档:

The sequence

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

is equivalent to

result = re.match(pattern, string)

已经提供了 pattern,因此在您的示例中:

pattern.match(pattern, hAndL)

相当于:

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

其中 re.match 的第三个参数是 flags,它必须是一个整数。相反,你应该这样做:

pattern.match(hAndL)