簡體   English   中英

幫助進行preg_match

[英]Help with preg_match

我正在嘗試解決僅在我們的PHP腳本從4.x移至5.x時出現的問題(是的,我知道)。 這是一個正則表達式,它在平面文件中搜索連續的條目塊並捕獲位於雙換行符(\\ n \\ n)分隔符之間的塊

這是preg_match代碼-找不到匹配項:

$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

任何見識將被荒謬地贊賞!

基思

的。 字符表示不是換行符的每個字符。 如果要允許換行符,則必須在正則表達式中添加“ s”選項。

preg_match("/\n\n(.*$nav_page: template=.*)\n\n/sU", $pages_blocks, $matches)

這符合您的要求。 我也刪除了? 添加U選項。

編輯:cdhowie的答案更好。 我完全忘記了“ m”選項。 我不會刪除我的內容只是為了向您顯示您的錯誤所在。
Edit2:實際上,兩個正則表達式都做不同的工作。 cdhowie匹配以$nav_page: template=開頭的任何行,而我的僅匹配位於大塊之一(即\\ n \\ n {your big block})中的行。

我認為問題在於模式中的雙\\n 但是,您實際上應該使用如下所示的內容:

preg_match("/^($nav_page: template=.*?)$/m", $pages_blocks, $matches)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM