Php 帮助准备比赛
我正在尝试修复一个问题,这个问题只有在我们的php脚本从4.x迁移到5.x时才会出现(是的,我知道)。它是一个正则表达式,用于在平面文件中搜索连续的条目块,并捕获位于双线分隔符之间的块(\n\n) 以下是preg_匹配代码-无法找到匹配项:Php 帮助准备比赛,php,preg-match,Php,Preg Match,我正在尝试修复一个问题,这个问题只有在我们的php脚本从4.x迁移到5.x时才会出现(是的,我知道)。它是一个正则表达式,用于在平面文件中搜索连续的条目块,并捕获位于双线分隔符之间的块(\n\n) 以下是preg_匹配代码-无法找到匹配项: $nav_page = 'business_notebook_plan_section1'; if (preg_match("/\n\n(.*?$nav_page: template=.*?)\n\n/", $pages_blocks, $matches)
$nav_page = 'business_notebook_plan_section1';
if (preg_match("/\n\n(.*?$nav_page: template=.*?)\n\n/", $pages_blocks, $matches)) {
// then find the block that contains this page
print "found a match!";
}
else {print "preg_match failed";}
以下是示例平面文件:
### BUSINESS ###
business_instructions: template=misc/instructions.html
business_notebook_intro: template=notebook/notebook.html&column2=1
business_notebook_plan_section1: template=notebook/notebook.html&column2=1
business_notebook_format_section1: template=notebook/notebook.html&column2=1
business_notebook_steps: template=notebook/notebook.html&column2=1&activity=poll
business_notebook_step1: template=notebook/notebook.html&column2=1
business_notebook_step2: template=notebook/notebook.html
business_notebook_step3: template=notebook/notebook.html&column2=1&activity=poll_text
business_notebook_step4: template=notebook/notebook.html&column2=1&activity=poll_text
business_notebook_step5_section1: template=notebook/notebook.html&column2=1
business_notebook_step6: template=notebook/notebook.html
business_notebook_step7: template=notebook/notebook.html&column2=1
business_notebook_review: template=notebook/notebook.html&num_questions=3
business_notebook_review_feedback: template=notebook/notebook.html&num_questions=3
business_notebook_summary: template=notebook/notebook.html&column2=1
business_notebook_intro_popup: template=shared/transcript_popup.html
business_notebook_intro_video: template=shared/video_popup.html
business_notebook_plan_section2: template=notebook/notebook.html&column2=1
business_notebook_steps_results: template=notebook/notebook.html&column2=1&activity=poll
business_notebook_format_section2: template=notebook/notebook.html&column2=1
business_notebook_step5_section2: template=notebook/notebook.html&column2=1
business_notebook_step5_section3: template=notebook/notebook.html&column2=1
business_notebook_step7_popup: template=shared/transcript_popup.html
business_notebook_step7_video: template=shared/video_popup.html
business_notebook_summary_popup: template=shared/transcript_popup.html
business_notebook_summary_video: template=shared/video_popup.html
business_resources: template=resources/resources.html&cells=2
business_preparation: template=preparation/preparation.html&cells=0
business_preparation_popup: template=preparation/preparation_popup.html&cells=0
任何洞察都会得到可笑的赞赏
基思我认为问题在于模式中的双重性。但是你真的应该用这样的东西来代替:
preg_match("/^($nav_page: template=.*?)$/m", $pages_blocks, $matches)
我认为问题在于模式中的双-
\n
。但是你真的应该用这样的东西来代替:
preg_match("/^($nav_page: template=.*?)$/m", $pages_blocks, $matches)
这个。字符是指不是新行字符的每个字符。如果要允许使用新行字符,则必须向regexp中添加“s”选项
preg_match("/\n\n(.*$nav_page: template=.*)\n\n/sU", $pages_blocks, $matches)
这符合你的要求。我还删除了?添加U选项
编辑:不过cdhowie的答案更好。我完全忘记了“m”选项。我不会删除我的,只是为了告诉你你的错误在哪里。Edit2:实际上,两个regexp的工作都不一样。cdhowie匹配以
$nav\u page:template=
开头的任何行,而我的仅匹配位于某个大块(即.\n\n{您的大块})中的行。字符表示不是新行字符的每个字符。如果要允许新行字符,则必须在regexp中添加“s”选项
preg_match("/\n\n(.*$nav_page: template=.*)\n\n/sU", $pages_blocks, $matches)
这符合您的要求。我还删除了?以添加U选项
编辑:不过,cdhowie的答案更好。我完全忘记了“m”选项。我不会删除我的选项,只是为了告诉你错误在哪里。Edit2:实际上,两个regexp的作用不同。cdhowie匹配任何以
$nav\u page:template=
开头的行,而我的只匹配位于其中一个大块中的行(即。\n\n{your big block}.在该行前后只有一个换行符。因此,将\n\n
实例更改为\n
可能会有所帮助…在该行前后只有一个换行符。因此,将\n\n
实例更改为\n
可能会有所帮助…为什么要将=
字符转义?这不是一个inpreg…Look-ahead断言:(?=\u pattern)。我很确定这是一个特殊的字符。但是你说得对,我在文档中找不到它,这很奇怪。我猜它毕竟不是元共享。只有在前面加上(?
或其他类似的Look-*标记时。重新阅读这个问题看起来像是可以做到的(PCRE\u DOTALL)…+1全部都是值得知道的。为什么要转义=
字符?它不是一个in-preg…Look-ahead断言:(?=\u pattern)。我很确定它是一个特殊的字符。但是你是对的,我在文档中找不到它,这很奇怪。我猜它毕竟不是元共享。只有在前面加(?
或另一个类似的look-*标记。重新阅读问题看起来像是可以做到的(PCRE_DOTALL)…+1总的来说是值得知道的。我+1想要这个,但事实上,我不确定OP想要什么。即使这一行是他文件的第一行,也会匹配。我+1想要这个,但事实上,我不确定OP想要什么。即使这一行是他文件的第一行,也会匹配。