{"id":52880,"date":"2022-03-17T12:52:11","date_gmt":"2022-03-17T12:52:11","guid":{"rendered":"https:\/\/merikebi.warrenmyers.com\/?p=52880"},"modified":"2022-03-17T12:52:11","modified_gmt":"2022-03-17T12:52:11","slug":"answer-by-warren-for-regular-expression-for-url-query-params-that-differ-in-size-order-and-topic","status":"publish","type":"post","link":"https:\/\/merikebi.warrenmyers.com\/?p=52880","title":{"rendered":"Answer by warren for regular expression for url query params that differ in size, order and topic"},"content":{"rendered":"<p>It <em>sounds<\/em> like you want to extract as many key-value pairs as there may be in the query portion of a URL<\/p>\n<p><em>If<\/em> this is correct, here&#8217;s a way to approach it:<\/p>\n<pre><code>index=ndx sourcetype=srctp url=*\n| eval query=split(url,&quot;?&quot;)\n| eval query=mvindex(query,-1)\n| rex field=query max_match=0 &quot;(?&lt;pair&gt;[^&amp;]+)&quot;\n| mvexpand pair\n<\/code><\/pre>\n<p>Alternatively, if you have a reason to want to split them out\/change the <code>=<\/code> to, say, a <code>|<\/code>:<\/p>\n<pre><code>index=ndx sourcetype=srctp url=*\n| eval query=split(url,&quot;?&quot;)\n| eval query=mvindex(query,-1)\n| rex field=query max_match=0 &quot;(?&lt;key&gt;[^=]+)\\=(?&lt;value&gt;[^=]+)&quot;\n| eval pair=mvzip(key,value,&quot; | &quot;)\n| mvexpand pair\n&lt;rest of search&gt;\n<\/code><\/pre>\n<p>By running <a href=\"https:\/\/docs.splunk.com\/Documentation\/Splunk\/latest\/SearchReference\/MultivalueEvalFunctions#split.28X.2C.22Y.22.29\" rel=\"nofollow noreferrer\"><code>split<\/code><\/a> on the URL against the <code>?<\/code> character, you&#8217;ll get the base URL and the query string (if there is any)<\/p>\n<p>Selecting the last index in the multivalue field (via <a href=\"https:\/\/docs.splunk.com\/Documentation\/Splunk\/latest\/SearchReference\/MultivalueEvalFunctions#mvindex.28MVFIELD.2CSTARTINDEX.2C_ENDINDEX.29\" rel=\"nofollow noreferrer\"><code>mvindex<\/code><\/a>), you&#8217;ll get <em>just<\/em> the query string, unless there was no query string, in which case you&#8217;ll get the base URL. If there were no query present, the first regular expression will return the entire URL as the <code>pair<\/code> field, and the second <code>rex<\/code> call should return nothing (since it won&#8217;t find an <code>=<\/code> sign in the URL).<\/p>\n<p>By using <code>max_match<\/code> with <a href=\"https:\/\/docs.splunk.com\/Documentation\/Splunk\/latest\/SearchReference\/rex#Optional_arguments\" rel=\"nofollow noreferrer\"><code>rex<\/code><\/a>, you&#8217;ll get back a [possibly] multivalue field of results<\/p>\n<p>Since all arguments in a URL query string have to be separated with a <code>&amp;<\/code>, this should work across most examples<\/p>\n<p>from User warren &#8211; Stack Overflow https:\/\/stackoverflow.com\/questions\/71511990\/regular-expression-for-url-query-params-that-differ-in-size-order-and-topic\/71512596#71512596<br \/>\nvia <a href=\"https:\/\/ifttt.com\/?ref=da&#038;site=wordpress\">IFTTT<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It sounds like you want to extract as many key-value pairs as there may be in the query portion of a URL If this is correct, here&#8217;s a way to approach it: index=ndx sourcetype=srctp url=* | eval query=split(url,&quot;?&quot;) | eval query=mvindex(query,-1) | rex field=query max_match=0 &quot;(?&lt;pair&gt;[^&amp;]+)&quot; | mvexpand pair Alternatively, if you have a reason &hellip;<br \/><a href=\"https:\/\/merikebi.warrenmyers.com\/?p=52880\" class=\"more-link pen_button pen_element_default pen_icon_arrow_double\">Continue reading <span class=\"screen-reader-text\">Answer by warren for regular expression for url query params that differ in size, order and topic<\/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-52880","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\/52880","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=52880"}],"version-history":[{"count":1,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts\/52880\/revisions"}],"predecessor-version":[{"id":52881,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts\/52880\/revisions\/52881"}],"wp:attachment":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=52880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=52880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=52880"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fkeyring_services&post=52880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}