{"id":33843,"date":"2023-02-14T14:58:39","date_gmt":"2023-02-14T09:28:39","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/"},"modified":"2023-02-14T14:58:39","modified_gmt":"2023-02-14T09:28:39","slug":"solved-preg_match-on-multiline-text","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/","title":{"rendered":"[Solved] Preg_match on multiline text"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-17028599\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"17028599\" data-parentid=\"17027927\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>The idea is to avoid the issue of new lines by not using the dot (<code>$subject<\/code> is your string):<\/p>\n<pre><code>$pattern = '~\n    Project\\hNo\\.\\h\\d++\\hDATE\\h\n    (?&lt;date&gt;\\d{1,2}\\\/\\d{1,2}\\\/\\d{1,2})\n    \\s++No\\.\\hQUESTION\\hANSWER\\s++\n    (?&lt;No&gt;\\d++)\\s++\n\n    # all characters but D or D not followed by \"ate Required\"\n    (?&lt;desc&gt;(?&gt;[^D]++|D(?!ate\\hRequired))+)\n\n    \\D++\n    (?&lt;date_required&gt;\\d{1,2}\\\/\\d{1,2}\\\/\\d{1,2})\n~x';\n\npreg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);\n\nprint_r($matches);\n<\/code><\/pre>\n<p>Note that i use <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.regular-expressions.info\/possessive.html%E2%80%8E\">possessive quantifiers<\/a> and <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.regular-expressions.info\/atomic.html%E2%80%8E\">atomic groups<\/a> to avoid as possible backtracks<\/p>\n<h3>EDIT:<\/h3>\n<p>According to your new example string, i give you a new type of pattern (in a kind of lex style) that is more readable and more editable:<\/p>\n<pre><code>$pattern = &lt;&lt;&lt;LOD\n~\n # Raw types\n (?(DEFINE)(?&lt;uint&gt;  \\d++                      ))\n (?(DEFINE)(?&lt;date&gt;  \\d{1,2}\\\/\\d{1,2}\\\/\\d{1,2} ))\n\n # Custom types\n (?(DEFINE)(?&lt;void&gt;  (?&gt;\\s++|&lt;br\\b[^&gt;]*+&gt;)*           ))\n (?(DEFINE)(?&lt;desc&gt;  (?&gt;[^D]++|D(?!ate\\h++Required))+ ))\n\n # Anchors\n (?(DEFINE)(?&lt;A_prj_date&gt;      PROJECT(?&gt;[^D]++|D(?!ATE\\b))+DATE\\h*+    ))\n (?(DEFINE)(?&lt;A_prj_number&gt;    \\g&lt;void&gt;No\\.\\h++QUESTION\\h++ANSWER\\b\\D++ ))\n (?(DEFINE)(?&lt;A_prj_desc&gt;      \\g&lt;void&gt;                                 ))\n (?(DEFINE)(?&lt;A_prj_date_req&gt;  Date\\h++Required\\D++                     ))\n\n # Pattern\n \\g&lt;A_prj_date&gt;     (?&lt;prj_date&gt;      \\g&lt;date&gt; )\n \\g&lt;A_prj_number&gt;   (?&lt;prj_number&gt;    \\g&lt;uint&gt; )\n \\g&lt;A_prj_desc&gt;     (?&lt;prj_desc&gt;      \\g&lt;desc&gt; )\n \\g&lt;A_prj_date_req&gt; (?&lt;prj_date_req&gt;  \\g&lt;date&gt; )    \n\n~xi\nLOD;\n<\/code><\/pre>\n<p>It begins with the definition of each component you need.<\/p>\n<ol>\n<li>Raw types: subpatterns that you can see everywhere<\/li>\n<li>Custom types: subpatterns specific to your project<\/li>\n<li>Anchors: subpatterns that describe the transition between required fields<\/li>\n<\/ol>\n<p>After, you have the pattern itself composed with these elements.<\/p>\n<p>You obtain something highly editable, since you can adapt all subpatterns to you needs, add new subpatterns and compose new subpatterns with others.<\/p>\n<p>Example, you can try to replace the <i>A_prj_number<\/i> subpattern with <code>\\D++<\/code> that seems to be good enough for your example string:<\/p>\n<pre><code>(?(DEFINE)(?&lt;A_prj_number&gt;\\D++))\n<\/code><\/pre>\n<p>Another advantage of this syntax, you can easily debug your pattern commenting one by one the elements (in the pattern section) from last to first until you obtain a match:<\/p>\n<pre><code># Pattern\n \\g&lt;A_prj_date&gt;     (?&lt;prj_date&gt;      \\g&lt;date&gt; )\n \\g&lt;A_prj_number&gt;   (?&lt;prj_number&gt;    \\g&lt;uint&gt; )\n # \\g&lt;A_prj_desc&gt;     (?&lt;prj_desc&gt;      \\g&lt;desc&gt; )\n # \\g&lt;A_prj_date_req&gt; (?&lt;prj_date_req&gt;  \\g&lt;date&gt; ) \n<\/code><\/pre>\n<p>note: if you have only one project by string use <i>preg_match<\/i> instead of <i>preg_match_all<\/i>.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Preg_match on multiline text <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] The idea is to avoid the issue of new lines by not using the dot ($subject is your string): $pattern = &#8216;~ Project\\hNo\\.\\h\\d++\\hDATE\\h (?&lt;date&gt;\\d{1,2}\\\/\\d{1,2}\\\/\\d{1,2}) \\s++No\\.\\hQUESTION\\hANSWER\\s++ (?&lt;No&gt;\\d++)\\s++ # all characters but D or D not followed by &#8220;ate Required&#8221; (?&lt;desc&gt;(?&gt;[^D]++|D(?!ate\\hRequired))+) \\D++ (?&lt;date_required&gt;\\d{1,2}\\\/\\d{1,2}\\\/\\d{1,2}) ~x&#8217;; preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER); print_r($matches); Note that i use possessive quantifiers and &#8230; <a title=\"[Solved] Preg_match on multiline text\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/\" aria-label=\"More on [Solved] Preg_match on multiline text\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[339,1952],"class_list":["post-33843","post","type-post","status-publish","format-standard","hentry","category-solved","tag-php","tag-preg-match"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Preg_match on multiline text - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Preg_match on multiline text - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] The idea is to avoid the issue of new lines by not using the dot ($subject is your string): $pattern = &#039;~ ProjecthNo.hd++hDATEh (?&lt;date&gt;d{1,2}\/d{1,2}\/d{1,2}) s++No.hQUESTIONhANSWERs++ (?&lt;No&gt;d++)s++ # all characters but D or D not followed by &quot;ate Required&quot; (?&lt;desc&gt;(?&gt;[^D]++|D(?!atehRequired))+) D++ (?&lt;date_required&gt;d{1,2}\/d{1,2}\/d{1,2}) ~x&#039;; preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER); print_r($matches); Note that i use possessive quantifiers and ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-14T09:28:39+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Preg_match on multiline text\",\"datePublished\":\"2023-02-14T09:28:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/\"},\"wordCount\":210,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"php\",\"preg-match\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/\",\"name\":\"[Solved] Preg_match on multiline text - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-02-14T09:28:39+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Preg_match on multiline text\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Preg_match on multiline text - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Preg_match on multiline text - JassWeb","og_description":"[ad_1] The idea is to avoid the issue of new lines by not using the dot ($subject is your string): $pattern = '~ ProjecthNo.hd++hDATEh (?&lt;date&gt;d{1,2}\/d{1,2}\/d{1,2}) s++No.hQUESTIONhANSWERs++ (?&lt;No&gt;d++)s++ # all characters but D or D not followed by \"ate Required\" (?&lt;desc&gt;(?&gt;[^D]++|D(?!atehRequired))+) D++ (?&lt;date_required&gt;d{1,2}\/d{1,2}\/d{1,2}) ~x'; preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER); print_r($matches); Note that i use possessive quantifiers and ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/","og_site_name":"JassWeb","article_published_time":"2023-02-14T09:28:39+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Preg_match on multiline text","datePublished":"2023-02-14T09:28:39+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/"},"wordCount":210,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["php","preg-match"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/","url":"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/","name":"[Solved] Preg_match on multiline text - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-02-14T09:28:39+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-preg_match-on-multiline-text\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Preg_match on multiline text"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/33843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=33843"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/33843\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=33843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=33843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=33843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}