{"id":61010,"date":"2022-07-26T20:02:04","date_gmt":"2022-07-26T20:02:04","guid":{"rendered":"https:\/\/merikebi.warrenmyers.com\/?p=61010"},"modified":"2022-07-26T20:02:04","modified_gmt":"2022-07-26T20:02:04","slug":"why-are-sequential-regular-expressions-more-efficient-than-a-combined-experession","status":"publish","type":"post","link":"https:\/\/merikebi.warrenmyers.com\/?p=61010","title":{"rendered":"Why are sequential regular expressions more efficient than a combined experession?"},"content":{"rendered":"<p>In <a href=\"https:\/\/stackoverflow.com\/a\/73128369\/4418\">answering<\/a> a Splunk question on SO, the following sample text was given:<\/p>\n<blockquote>\n<p>msg: abc.asia &#8211; [2021-08-23T00:27:08.152+0000] &quot;GET \/facts?factType=COMMERCIAL&amp;sourceSystem=ADMIN&amp;sourceOwner=ABC&amp;filters=%257B%2522stringMatchFilters%2522:%255B%257B%2522key%2522:%2522BFEESCE((json_data-%253E%253E&#8217;isNotSearchable&#8217;)::boolean,%2520false)%2522,%2522value%2522:%2522false%2522,%2522operator%2522:%2522EQ%2522%257D%255D,%2522multiStringMatchFilters%2522:%255B%257B%2522key%2522:%2522json_data-%253E%253E&#8217;id&#8217;%2522,%2522values%2522:%255B%25224970111%2522%255D%257D%255D,%2522containmentFilters%2522:%255B%255D,%2522nestedMultiStringMatchFilter%2522:%255B%255D,%2522nestedStringMatchFilters%2522:%255B%255D%257D&amp;sorts=%257B%2522sortOrders%2522:%255B%257B%2522key%2522:%2522id%2522,%2522order%2522:%2522DESC%2522%257D%255D%257D&amp;pagination=null<\/p>\n<\/blockquote>\n<p>The person wanted to extract everything in the &quot;filters&quot; portion of the URL if &quot;factType&quot; was &quot;COMMERCIAL&quot;<\/p>\n<p>The following all-in-one regex pulls it out neatly (presuming the URL is always in the right order (ie <code>factType<\/code> coming before <code>filters<\/code>):<\/p>\n<pre><code>factType=(?&lt;facttype&gt;\\w+).+filters=(?&lt;filters&gt;[^\\&amp;]+)\n<\/code><\/pre>\n<p>According to <a href=\"https:\/\/regex101.com\/r\/3rHvUq\/1\" rel=\"nofollow noreferrer\">regex101<\/a>, it finds its expected matches with 670 steps<\/p>\n<p>But if I break it up to<\/p>\n<pre><code>factType=(?&lt;facttype&gt;\\w+)\n<\/code><\/pre>\n<p>followed by<\/p>\n<pre><code>filters=(?&lt;filters&gt;[^\\&amp;]+)\n<\/code><\/pre>\n<p>regex101 reports the matches being found with 26 and 16 steps, respectively<\/p>\n<p>What about breaking up the regex into two makes it so much more (~15x) efficient to match?<\/p>\n<p>from User warren &#8211; Stack Overflow https:\/\/stackoverflow.com\/questions\/73129119\/why-are-sequential-regular-expressions-more-efficient-than-a-combined-experessio<br \/>\nvia <a href=\"https:\/\/ifttt.com\/?ref=da&#038;site=wordpress\">IFTTT<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In answering a Splunk question on SO, the following sample text was given: msg: abc.asia &#8211; [2021-08-23T00:27:08.152+0000] &quot;GET \/facts?factType=COMMERCIAL&amp;sourceSystem=ADMIN&amp;sourceOwner=ABC&amp;filters=%257B%2522stringMatchFilters%2522:%255B%257B%2522key%2522:%2522BFEESCE((json_data-%253E%253E&#8217;isNotSearchable&#8217;)::boolean,%2520false)%2522,%2522value%2522:%2522false%2522,%2522operator%2522:%2522EQ%2522%257D%255D,%2522multiStringMatchFilters%2522:%255B%257B%2522key%2522:%2522json_data-%253E%253E&#8217;id&#8217;%2522,%2522values%2522:%255B%25224970111%2522%255D%257D%255D,%2522containmentFilters%2522:%255B%255D,%2522nestedMultiStringMatchFilter%2522:%255B%255D,%2522nestedStringMatchFilters%2522:%255B%255D%257D&amp;sorts=%257B%2522sortOrders%2522:%255B%257B%2522key%2522:%2522id%2522,%2522order%2522:%2522DESC%2522%257D%255D%257D&amp;pagination=null The person wanted to extract everything in the &quot;filters&quot; portion of the URL if &quot;factType&quot; was &quot;COMMERCIAL&quot; The following all-in-one regex pulls it out neatly (presuming the URL is always in the right order (ie factType &hellip;<br \/><a href=\"https:\/\/merikebi.warrenmyers.com\/?p=61010\" class=\"more-link pen_button pen_element_default pen_icon_arrow_double\">Continue reading <span class=\"screen-reader-text\">Why are sequential regular expressions more efficient than a combined experession?<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[991],"keyring_services":[],"class_list":["post-61010","post","type-post","status-publish","format-standard","hentry","category-blih","tag-stackexchange"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts\/61010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=61010"}],"version-history":[{"count":1,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts\/61010\/revisions"}],"predecessor-version":[{"id":61011,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts\/61010\/revisions\/61011"}],"wp:attachment":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=61010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=61010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=61010"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fkeyring_services&post=61010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}