<?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: How to extract a field using rex that may or may not be present? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-field-using-rex-that-may-or-may-not-be-present/m-p/359240#M106205</link>
    <description>&lt;P&gt;thanks @cpetterborg. So with &lt;CODE&gt;?&lt;/CODE&gt; instead of &lt;CODE&gt;*&lt;/CODE&gt; will improve performance in this case..updated the answer.&lt;/P&gt;</description>
    <pubDate>Wed, 07 Feb 2018 13:50:28 GMT</pubDate>
    <dc:creator>493669</dc:creator>
    <dc:date>2018-02-07T13:50:28Z</dc:date>
    <item>
      <title>How to extract a field using rex that may or may not be present?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-field-using-rex-that-may-or-may-not-be-present/m-p/359236#M106201</link>
      <description>&lt;P&gt;Consider I am having two string - &lt;CODE&gt;"YY02State"&lt;/CODE&gt; and &lt;CODE&gt;"Y02State"&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;In the above strings, I have to extract the fields like:&lt;BR /&gt;
Y - &lt;CODE&gt;IsStateLegal&lt;/CODE&gt;&lt;BR /&gt;
Y - &lt;CODE&gt;IsStateSafe&lt;/CODE&gt;&lt;BR /&gt;
02 - &lt;CODE&gt;StateId&lt;/CODE&gt;&lt;BR /&gt;
State - &lt;CODE&gt;NameOfState&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;There might be instances when the &lt;CODE&gt;IsStateSafe&lt;/CODE&gt; field is not available in the log entry, like it is in the second string &lt;CODE&gt;"Y02State"&lt;/CODE&gt;. How can I write rex for this? Please note the other fields will always be available.&lt;/P&gt;

&lt;P&gt;I tried the following rex, but of no luck.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;(?&amp;lt;IsStateLegal&amp;gt;\w{1})(?&amp;lt;IsStateSafe&amp;gt;\w*.{1})(?&amp;lt;StateId&amp;gt;d{2})(?&amp;lt;NameOfState&amp;gt;\w*)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Please suggest a solution for this.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Feb 2018 06:00:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-field-using-rex-that-may-or-may-not-be-present/m-p/359236#M106201</guid>
      <dc:creator>Naren26</dc:creator>
      <dc:date>2018-02-07T06:00:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a field using rex that may or may not be present?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-field-using-rex-that-may-or-may-not-be-present/m-p/359237#M106202</link>
      <description>&lt;P&gt;Try this run anywhere search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; |makeresults|eval _raw="Y02State"|rex "(?&amp;lt;IsStateLegal&amp;gt;\w{1})(?&amp;lt;IsStateSafe&amp;gt;\w)?(?&amp;lt;StateId&amp;gt;\d{2})(?&amp;lt;NameOfState&amp;gt;\w+)"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Feb 2018 06:07:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-field-using-rex-that-may-or-may-not-be-present/m-p/359237#M106202</guid>
      <dc:creator>493669</dc:creator>
      <dc:date>2018-02-07T06:07:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a field using rex that may or may not be present?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-field-using-rex-that-may-or-may-not-be-present/m-p/359238#M106203</link>
      <description>&lt;P&gt;hey @Naren26,&lt;/P&gt;

&lt;P&gt;Try this run anywhere search&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval raw="YY02State Y02State N32State YN02State" 
| makemv raw 
| mvexpand raw 
| rex field=raw "(?&amp;lt;IsStateLegal&amp;gt;[A-Za-z])(?&amp;lt;IsStateSafe&amp;gt;[^\d]*)(?&amp;lt;StateId&amp;gt;\d{2})(?&amp;lt;NameOfState&amp;gt;\S+)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;In your environment, you should write&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| rex field=_raw "(?&amp;lt;IsStateLegal&amp;gt;[A-Za-z])(?&amp;lt;IsStateSafe&amp;gt;[^\d]*)(?&amp;lt;StateId&amp;gt;\d{2})(?&amp;lt;NameOfState&amp;gt;\S+)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;let me know if this helps!&lt;/P&gt;</description>
      <pubDate>Wed, 07 Feb 2018 06:47:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-field-using-rex-that-may-or-may-not-be-present/m-p/359238#M106203</guid>
      <dc:creator>mayurr98</dc:creator>
      <dc:date>2018-02-07T06:47:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a field using rex that may or may not be present?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-field-using-rex-that-may-or-may-not-be-present/m-p/359239#M106204</link>
      <description>&lt;PRE&gt;&lt;CODE&gt;|makeresults|eval _raw="Y02State"|rex "(?&amp;lt;IsStateLegal&amp;gt;\w{1})(?&amp;lt;IsStateSafe&amp;gt;\w)?(?&amp;lt;StateId&amp;gt;\d{2})(?&amp;lt;NameOfState&amp;gt;\w+)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This will result in half the number of steps required to match. The greedy * makes it work twice as much in this case.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Feb 2018 12:54:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-field-using-rex-that-may-or-may-not-be-present/m-p/359239#M106204</guid>
      <dc:creator>cpetterborg</dc:creator>
      <dc:date>2018-02-07T12:54:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a field using rex that may or may not be present?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-field-using-rex-that-may-or-may-not-be-present/m-p/359240#M106205</link>
      <description>&lt;P&gt;thanks @cpetterborg. So with &lt;CODE&gt;?&lt;/CODE&gt; instead of &lt;CODE&gt;*&lt;/CODE&gt; will improve performance in this case..updated the answer.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Feb 2018 13:50:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-field-using-rex-that-may-or-may-not-be-present/m-p/359240#M106205</guid>
      <dc:creator>493669</dc:creator>
      <dc:date>2018-02-07T13:50:28Z</dc:date>
    </item>
  </channel>
</rss>

