<?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 Using a lookup table to store regex patterns to be used in a search in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Using-a-lookup-table-to-store-regex-patterns-to-be-used-in-a/m-p/673081#M230497</link>
    <description>&lt;P&gt;Is it possible to store regex patterns in a lookup table so that it can be used in a search?&lt;/P&gt;&lt;P&gt;For example lets say I have these following regexes like "(?&amp;lt;regex1&amp;gt;hello)" and "(?&amp;lt;regex2&amp;gt;world)".&amp;nbsp; My actual regexes are not simple word matches.&lt;/P&gt;&lt;P&gt;I want to write another query that basically runs a bunch of regexes like&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| rex field=data "regex1"
| rex field=data "regex2"
etc &lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;| makeresults 1 | eval data="Hello world" [| inputlookup regex.csv | streamstats count | strcat "| rex field=data \"" regex "\"" as regexstring | table regexstring  | mvcombine regexstring]​&lt;/LI-CODE&gt;&lt;P&gt;is it possible to use the subsearch to extract the regexes and then use them as commands in the main query?&lt;/P&gt;&lt;P&gt;I was trying something like&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults 1 | eval data="Hello world" [| inputlookup regex.csv | streamstats count | strcat "| rex field=data \"" regex "\"" as regexstring | table regexstring  | mvcombine regexstring]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;so that the subsearch outputs the following&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| rex field=data "(?&amp;lt;regex1&amp;gt;hello)"
| rex field=data "(?&amp;lt;regex2&amp;gt;world)"&lt;/LI-CODE&gt;</description>
    <pubDate>Tue, 02 Jan 2024 22:37:35 GMT</pubDate>
    <dc:creator>klim</dc:creator>
    <dc:date>2024-01-02T22:37:35Z</dc:date>
    <item>
      <title>Using a lookup table to store regex patterns to be used in a search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-a-lookup-table-to-store-regex-patterns-to-be-used-in-a/m-p/673081#M230497</link>
      <description>&lt;P&gt;Is it possible to store regex patterns in a lookup table so that it can be used in a search?&lt;/P&gt;&lt;P&gt;For example lets say I have these following regexes like "(?&amp;lt;regex1&amp;gt;hello)" and "(?&amp;lt;regex2&amp;gt;world)".&amp;nbsp; My actual regexes are not simple word matches.&lt;/P&gt;&lt;P&gt;I want to write another query that basically runs a bunch of regexes like&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| rex field=data "regex1"
| rex field=data "regex2"
etc &lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;| makeresults 1 | eval data="Hello world" [| inputlookup regex.csv | streamstats count | strcat "| rex field=data \"" regex "\"" as regexstring | table regexstring  | mvcombine regexstring]​&lt;/LI-CODE&gt;&lt;P&gt;is it possible to use the subsearch to extract the regexes and then use them as commands in the main query?&lt;/P&gt;&lt;P&gt;I was trying something like&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults 1 | eval data="Hello world" [| inputlookup regex.csv | streamstats count | strcat "| rex field=data \"" regex "\"" as regexstring | table regexstring  | mvcombine regexstring]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;so that the subsearch outputs the following&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| rex field=data "(?&amp;lt;regex1&amp;gt;hello)"
| rex field=data "(?&amp;lt;regex2&amp;gt;world)"&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 02 Jan 2024 22:37:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-a-lookup-table-to-store-regex-patterns-to-be-used-in-a/m-p/673081#M230497</guid>
      <dc:creator>klim</dc:creator>
      <dc:date>2024-01-02T22:37:35Z</dc:date>
    </item>
    <item>
      <title>Re: Using a lookup table to store regex patterns to be used in a search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-a-lookup-table-to-store-regex-patterns-to-be-used-in-a/m-p/673083#M230498</link>
      <description>&lt;P&gt;I can get it working to an extent, not sure if this method will exactly fit your use-case but will leave it here for you.&lt;BR /&gt;&lt;BR /&gt;So with a lookup named "test_regex_lookup.csv"&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;pattern_type&lt;/TD&gt;&lt;TD&gt;regex&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="50%"&gt;date&lt;/TD&gt;&lt;TD width="50%"&gt;&lt;SPAN&gt;\d{2}\/\d{2}\/\d{4}&lt;/SPAN&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="50%"&gt;SSN&lt;/TD&gt;&lt;TD width="50%"&gt;&amp;nbsp;&lt;SPAN&gt;\d{3}\-\d{2}\-\d{4}&lt;/SPAN&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;We are able to pull in these regex patterns into a parent search via eval and then use these patterns in another eval to extract data.&lt;BR /&gt;&lt;BR /&gt;Example.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults 
    | eval
        data="very personal info on John Doe: Birthday: 04/12/1973 and SSN: 123-45-6789"
    ``` pull in regex patterns from lookup ```
    | eval
        ssn_regex=[ | inputlookup test_regex_lookup.csv where pattern_type="SSN" | fields + regex | eval regex="\"".'regex'."\"" | return $regex ],
        bday_regex=[ | inputlookup test_regex_lookup.csv where pattern_type="date" | fields + regex | eval regex="\"".'regex'."\"" | return $regex ]
    ``` use regex pattern fields to extract matches from another field "data" ```
    | eval
        ssn=replace(data, ".*(".'ssn_regex'.").*", "\1"),
        bday=replace(data, ".*(".'bday_regex'.").*", "\1")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Resulting dataset looks something like this&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dtburrows3_0-1704236821679.png" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/28730i018F81598DD4BBE4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dtburrows3_0-1704236821679.png" alt="dtburrows3_0-1704236821679.png" /&gt;&lt;/span&gt;&lt;BR /&gt;I'm sure there are other methods that can work or we can build upon this method further.&lt;BR /&gt;&lt;BR /&gt;I am curious about different ways of doing this as well so will leave updates if I figure out any other methods.&lt;/P&gt;&lt;P&gt;Update:&lt;BR /&gt;Was able to shorten the SPL into a single eval by using the nifty lookup() function&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults 
    | eval
        data="very personal info on John Doe: Birthday: 04/12/1973 and SSN: 123-45-6789"
    ``` get regex pattern from lookup and utilize against raw data in another field to extract data into net-new field ```
    | eval 
        ssn=replace(data, ".*(".spath(lookup("test_regex_lookup.csv", json_object("pattern_type", "SSN"), json_array("regex")), "regex").").*", "\1"),
        bday=replace(data, ".*(".spath(lookup("test_regex_lookup.csv", json_object("pattern_type", "date"), json_array("regex")), "regex").").*", "\1")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dtburrows3_0-1704237940483.png" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/28731i14A63401CBA2A80F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dtburrows3_0-1704237940483.png" alt="dtburrows3_0-1704237940483.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jan 2024 23:26:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-a-lookup-table-to-store-regex-patterns-to-be-used-in-a/m-p/673083#M230498</guid>
      <dc:creator>dtburrows3</dc:creator>
      <dc:date>2024-01-02T23:26:13Z</dc:date>
    </item>
    <item>
      <title>Re: Using a lookup table to store regex patterns to be used in a search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-a-lookup-table-to-store-regex-patterns-to-be-used-in-a/m-p/673085#M230499</link>
      <description>&lt;P&gt;That is a pretty good solution. But I was looking for something that wouldn't require updating the query if another regex is added to the list.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jan 2024 23:42:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-a-lookup-table-to-store-regex-patterns-to-be-used-in-a/m-p/673085#M230499</guid>
      <dc:creator>klim</dc:creator>
      <dc:date>2024-01-02T23:42:50Z</dc:date>
    </item>
    <item>
      <title>Re: Using a lookup table to store regex patterns to be used in a search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-a-lookup-table-to-store-regex-patterns-to-be-used-in-a/m-p/673087#M230500</link>
      <description>&lt;P&gt;Not the prettiest solution but shouldn't require updating since its looping through all regexes from the lookup using a mvmap() function.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults 
    | eval
        data="very personal info on John Doe: Birthday: 04/12/1973 and SSN: 123-45-6789"
    | join type=left
        [ | inputlookup test_regex_lookup.csv | tojson str(pattern_type) str(regex) output_field=regex_json | stats values(regex_json) as regex_json | eval regex_array=mv_to_json_array(regex_json) | fields + regex_array ]
    | eval
        regex_json=json_array_to_mv(regex_array)
    | fields - regex_array
    | eval
        regex_patterns=case(
            mvcount(regex_json)==1, spath(regex_json, "regex"), 
            mvcount(regex_json)&amp;gt;1, mvmap(regex_json, spath(regex_json, "regex"))
            )
    | eval
        regex_match_json=case(
            mvcount(regex_json)==1, 
                if(match(data, spath(regex_json, "regex")), json_set(regex_json, "matches", replace(data, ".*(".spath(regex_json, "regex").").*", "\1")), null()),
            mvcount(regex_json)&amp;gt;1, mvmap(regex_json, if(match(data, spath(regex_json, "regex")), json_set(regex_json, "matches", replace(data, ".*(".spath(regex_json, "regex").").*", "\1")), null()))
            )
    | fields - regex_json, regex_patterns
    | eval
        all_regex_matches=mvmap(regex_match_json, spath(regex_match_json, "matches"))&lt;/LI-CODE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dtburrows3_0-1704241009533.png" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/28732iADC1DF438E801E16/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dtburrows3_0-1704241009533.png" alt="dtburrows3_0-1704241009533.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Also makes another field (regex_match_json) to map back the pattern that matched the extraction for reference.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jan 2024 00:19:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-a-lookup-table-to-store-regex-patterns-to-be-used-in-a/m-p/673087#M230500</guid>
      <dc:creator>dtburrows3</dc:creator>
      <dc:date>2024-01-03T00:19:07Z</dc:date>
    </item>
    <item>
      <title>Re: Using a lookup table to store regex patterns to be used in a search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-a-lookup-table-to-store-regex-patterns-to-be-used-in-a/m-p/673092#M230502</link>
      <description>&lt;P&gt;Alright I figured you would want the fields extracted with their intended fieldnames instead of any-and-all matches being contained in a single multivalue field so here is SPL to do that.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;base_search&amp;gt;
    ``` this SPL required a field named "data" containing a raw string as its value ```
    ``` this can be macroed by replacing the input field "data" and lookup name "test_regex_lookup.csv" ```
    ``` example: | `extract_regex_from_lookup(data, test_regex_lookup.csv)` ```
    ``` pull in all regex patterns as an array of json objects into the parent search as a new field ```
    | join type=left
        [ | inputlookup test_regex_lookup.csv | tojson str(pattern_type) str(regex) output_field=regex_json | stats values(regex_json) as regex_json | eval regex_array=mv_to_json_array(regex_json) | fields + regex_array ]
    ``` parse array of json objects into a multivalued field of json objects ```
    | eval
        regex_json=json_array_to_mv(regex_array)
    ``` remove array (no longer needed) ```
    | fields - regex_array
    ``` search the raw text of field "data" for matches against any of the regex patterns contained in the regex_json multivalue field ```
    | eval
        regex_match_json=case(
            mvcount(regex_json)==1, 
                if(match(data, spath(regex_json, "regex")), json_set(regex_json, "matches", replace(data, ".*(".spath(regex_json, "regex").").*", "\1")), null()),
            mvcount(regex_json)&amp;gt;1, mvmap(regex_json, if(match(data, spath(regex_json, "regex")), json_set(regex_json, "matches", replace(data, ".*(".spath(regex_json, "regex").").*", "\1")), null()))
            )
    ``` remove regex_json (no longer needed) ```
    | fields - regex_json
    ``` (optional) multivalued field containing all pattern matches ```
    | eval
        all_regex_matches=mvmap(regex_match_json, spath(regex_match_json, "matches"))
    ``` create temporary json object to hold key/value pairs for pattern_type attribution ```
    | eval
        tmp_json=json_object()
    ``` loop through the regex_match_json multivalue field and assign a key/value entry to "tmp_json" for the (pattern_type: matches) ```
    | foreach mode=multivalue regex_match_json
        [
            | eval
                tmp_json=json_set(tmp_json, spath('&amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;', "pattern_type"), spath('&amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;', "matches"))
            ]
    ``` full spath against tmp_json to get field extractions for all matches against the pattern_types ```
    | spath input=tmp_json
    ``` remove temporary json object (no loger needed) ```
    | fields - tmp_json
    ``` (optional) remove regex_match_json field ```
    | fields - regex_match_json
    ``` end of `extract_regex_from_lookup(2)` macro ```
    ``` table all extracted fields derived from "data" field and regex stored in lookup "test_regex_lookup.csv" ```
    | table _time, data, all_regex_matches, *&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;I am pretty happy with how this turned out but there may be an easier way of doing it.&lt;BR /&gt;Would be glad to hear anybody else to chime in on an easier way of accomplishing this.&lt;BR /&gt;I have just always had problems with piping in data from a lookup into a parent search as executable SPL other than pulling it into an eval of some sort.&lt;BR /&gt;&lt;BR /&gt;Reference screenshot of sample output&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dtburrows3_0-1704257016272.png" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/28733iB2334095A77ED6BB/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dtburrows3_0-1704257016272.png" alt="dtburrows3_0-1704257016272.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;So the current SPL will assign the match to its corresponding row's pattern_type value from the lookup as a fieldname.&lt;BR /&gt;In this example it is SSN, date, and name.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jan 2024 04:46:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-a-lookup-table-to-store-regex-patterns-to-be-used-in-a/m-p/673092#M230502</guid>
      <dc:creator>dtburrows3</dc:creator>
      <dc:date>2024-01-03T04:46:34Z</dc:date>
    </item>
  </channel>
</rss>

