为什么此正则表达式会返回错误? [英] Why would this regex return an error?

查看:136
本文介绍了为什么此正则表达式会返回错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if(preg_match_all('%<tr.*?>.*?<b>.*?</b>.*?</tr>%ims', $contents, $x)===FALSE)
{...}

$contents是使用file_get_contents()此来源中检索的

正则表达式经过简化,可以解决问题.我实际使用的代码是:

The regex was simplified to troublshoot the problem. The code I was actually using was:

if(preg_match(
           '%Areas of Study: </P>.*?<TABLE BORDER="0">(.*?)<TBODY>.*?</TBODY>.*?   </TABLE>%ims',
            $contents, $course_list)
    )
    {
        if(preg_match_all('%<TR>.*?<TD.*?>.*?<B>(.*?)</B>.*?</TD>.*?<TD.*?>.*?</TD>.*?<TD.*?>.*?<B>(.*?)</B>.*?</TD>.*?</TR>%ims',
                $course_list[0], $course_titles)
        )
        {
            ...
        }
        else 
        {
            die('<p>ERROR: first preg_match_all fails</p>');
        }

        echo '<p>INFO: Courses  found</p>';
    }
    else
    {
        die('<p>ERROR: Courses not found</p>');
    }

    if(
        preg_match_all('%<tr.*?>.*?<b>.*?first '.$college.' area of study.*?</b>.*?</tr>.*?<tr.*?>.*?<td.*?>.*?<b>(.*?) \((.*?)\).*?</b>(.*?credits.*?)</td>.*?<td.*?>(.*?<a .*?)</td>.*?</tr>%ims',
        $contents, $course_modules))
    {
        ....
    }
    else
    {
        die('<p>ERROR: Courses details/streams not found</p>');
    }

我总是得到:

信息:找到的课程
错误:找不到课程详情/信息流

INFO: Courses found
ERROR: Course detail/streams not found

奇怪的是,其他正则表达式函数调用似乎起作用了,但最后一个调用却不起作用.

It's strange how the other regex function calls seem to work but not the last one.

注意:

此正则表达式以前有效(实际上更复杂).我不确定这是否重要,但是我更新了WAMP版本(因此我的php.ini等已重置),上周我在解决MongoDB连接问题时弄乱了我的设置

This regex previously worked (it was actually more complex). I'm not sure if this matters but but I updated my version of WAMP (therefore my php.ini, etc. was reset) and I messed around with my setup while troubleshooting a MongoDB connection problem last week.

推荐答案

您可以检查

You might check your pcre.backtrack_limit setting. It would have to be ridiculously low to prevent that regex from matching that input, but you did say you'd been messing around with the setup...

您可以尝试通过更改正则表达式来对其进行测试.当我在RegexBuddy中对其进行测试时,您的正则表达式将在1216步中匹配该输入.当我将其更改为此:

You can try testing it by changing the regex. When I tested it in RegexBuddy, your regex matched that input in 1216 steps. When I changed it to this:

'%<tr.*?>.*?<b>.*?</b>[^<]*(?:<(?!/?tr\b)[^<]*)*</tr>%ims'

...只走了441步.

...it only took 441 steps.

这篇关于为什么此正则表达式会返回错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆