<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Using regex function with pattern stored in a variable in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-function-with-pattern-stored-in-a-variable/m-p/150146#M42057</link>
    <description>&lt;P&gt;can you try to cast the variable in a field with an eval first ?&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;sourcetype = mysourcetype | eval mycategory=$app_category$ | eval catregex = case(match(mycategory,"category1"),"^(my|reg|ex)" | regex name = catregex&lt;/CODE&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 14 Feb 2014 02:06:50 GMT</pubDate>
    <dc:creator>yannK</dc:creator>
    <dc:date>2014-02-14T02:06:50Z</dc:date>
    <item>
      <title>Using regex function with pattern stored in a variable</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-function-with-pattern-stored-in-a-variable/m-p/150145#M42056</link>
      <description>&lt;P&gt;I have dashboard panel with a dropdown menu on it.  When the user selects a category from the dropdown, it will be stored in the variable $category$.&lt;/P&gt;

&lt;P&gt;Based on the category selected by the user, I want to apply a regular expression to the "name" field in my search.&lt;/P&gt;

&lt;P&gt;For example, if the user selects the category "category1", then I want to apply the regular expression "^(my|reg|ex)" to the "name" field in my search.&lt;/P&gt;

&lt;P&gt;Here's what I tried:&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;sourcetype = mysourcetype | eval catregex = case(match($app_category$,"category1"),"^(my|reg|ex)" | regex name = catregex&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;This is not working.  I'm thinking that the problem is with the command &lt;CODE&gt;regex name = catregex&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Maybe the field/variable that I created, catregex, is being interpreted as a string in that context?  If so, how can I make sure it's interpreted as a variable?&lt;/P&gt;

&lt;P&gt;I also tried something like this with &lt;CODE&gt;regex&lt;/CODE&gt; command, but it seems this is not possible:&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;regex name = case(match($app_category$,"category1"),"^(my|reg|ex)"&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Any help would be greatly appreciated!&lt;/P&gt;

&lt;P&gt;Thanks, fellow Splunkers!&lt;/P&gt;</description>
      <pubDate>Fri, 14 Feb 2014 01:44:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-regex-function-with-pattern-stored-in-a-variable/m-p/150145#M42056</guid>
      <dc:creator>dave_krebs</dc:creator>
      <dc:date>2014-02-14T01:44:03Z</dc:date>
    </item>
    <item>
      <title>Re: Using regex function with pattern stored in a variable</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-function-with-pattern-stored-in-a-variable/m-p/150146#M42057</link>
      <description>&lt;P&gt;can you try to cast the variable in a field with an eval first ?&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;sourcetype = mysourcetype | eval mycategory=$app_category$ | eval catregex = case(match(mycategory,"category1"),"^(my|reg|ex)" | regex name = catregex&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Feb 2014 02:06:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-regex-function-with-pattern-stored-in-a-variable/m-p/150146#M42057</guid>
      <dc:creator>yannK</dc:creator>
      <dc:date>2014-02-14T02:06:50Z</dc:date>
    </item>
    <item>
      <title>Re: Using regex function with pattern stored in a variable</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-function-with-pattern-stored-in-a-variable/m-p/150147#M42058</link>
      <description>&lt;P&gt;I eventually figured this one out.  The fix is to put &lt;CODE&gt;$app_category$&lt;/CODE&gt; in double quotes:&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;eval mycategory="$app_category$"&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 07 May 2014 13:45:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-regex-function-with-pattern-stored-in-a-variable/m-p/150147#M42058</guid>
      <dc:creator>dave_krebs</dc:creator>
      <dc:date>2014-05-07T13:45:15Z</dc:date>
    </item>
  </channel>
</rss>

