<?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 to extract a string where the following string may or may not exist in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293311#M88544</link>
    <description>&lt;P&gt;I am partly wondering if the ".+" may be part of the issue. Given the content of  can be varied and contain spaces and special characters I am not sure how to get around that.&lt;/P&gt;</description>
    <pubDate>Wed, 11 Oct 2017 03:04:52 GMT</pubDate>
    <dc:creator>rhysjones</dc:creator>
    <dc:date>2017-10-11T03:04:52Z</dc:date>
    <item>
      <title>Using regex to extract a string where the following string may or may not exist</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293310#M88543</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I am trying to extract some fields which are generally bound by other strings (eg Some Text 1  Some Text 2). I have a situation where a field may or may not have anything following it.&lt;/P&gt;

&lt;P&gt;For example, with this data set :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;1 Some Text 1 &amp;lt;my field 1&amp;gt; Some Text 2
2 Some Text 1 &amp;lt;my field 1&amp;gt;",
3 Some Text 1 &amp;lt;my field 1&amp;gt; Some Text 2
4 Some Text 1 &amp;lt;my field 1&amp;gt; Some Text 2
5 Some Text 1 &amp;lt;my field 1&amp;gt;",
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This regex partly works in that is extracts correctly items 1, 3, and 4:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Some Text 1\s+(?P&amp;lt;my field 1&amp;gt;.+)\s(Some Text 2|\",)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This regex partly works in that is extracts correctly items 2 and 5, but extracts the entirety of items 1, 3, and 4.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Some Text 1\s+(?P&amp;lt;my field 1&amp;gt;.+)(Some Text 2|\",)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The difference is the "\s". I can't seem to include that in the match group, only before it.&lt;/P&gt;

&lt;P&gt;I am sure I am missing something obvious but can't seem to see it. Any help much appreciated.&lt;/P&gt;

&lt;P&gt;Thankyou.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Oct 2017 02:53:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293310#M88543</guid>
      <dc:creator>rhysjones</dc:creator>
      <dc:date>2017-10-11T02:53:33Z</dc:date>
    </item>
    <item>
      <title>Re: Using regex to extract a string where the following string may or may not exist</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293311#M88544</link>
      <description>&lt;P&gt;I am partly wondering if the ".+" may be part of the issue. Given the content of  can be varied and contain spaces and special characters I am not sure how to get around that.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Oct 2017 03:04:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293311#M88544</guid>
      <dc:creator>rhysjones</dc:creator>
      <dc:date>2017-10-11T03:04:52Z</dc:date>
    </item>
    <item>
      <title>Re: Using regex to extract a string where the following string may or may not exist</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293312#M88545</link>
      <description>&lt;P&gt;I'm a bit confused by what you want in the end. Is this what you want to see:&lt;/P&gt;

&lt;P&gt;&lt;A href="https://regex101.com/r/xkvSzf/1"&gt;https://regex101.com/r/xkvSzf/1&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Oct 2017 03:08:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293312#M88545</guid>
      <dc:creator>cpetterborg</dc:creator>
      <dc:date>2017-10-11T03:08:41Z</dc:date>
    </item>
    <item>
      <title>Re: Using regex to extract a string where the following string may or may not exist</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293313#M88546</link>
      <description>&lt;P&gt;Spot on. 5 Matches regardless of whether there is a string following, or a ", following. &lt;BR /&gt;
That construct does not appear to be working in Splunk (or in my dataset). For example, if I put the \s inside the match brackets then it seems to be ignored and that side of the match fails.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Oct 2017 03:56:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293313#M88546</guid>
      <dc:creator>rhysjones</dc:creator>
      <dc:date>2017-10-11T03:56:10Z</dc:date>
    </item>
    <item>
      <title>Re: Using regex to extract a string where the following string may or may not exist</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293314#M88547</link>
      <description>&lt;P&gt;I don't know if you noticed, but the name I used in the capture group doesn't have spaces. That is a requirement - no spaces in capture group names. I don't know if that might be causing things to not work for you. You could also just try a space character instead of a &lt;CODE&gt;\s&lt;/CODE&gt;. I'm not sure if either of those will help, but they are worth a try.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Oct 2017 04:20:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293314#M88547</guid>
      <dc:creator>cpetterborg</dc:creator>
      <dc:date>2017-10-11T04:20:02Z</dc:date>
    </item>
    <item>
      <title>Re: Using regex to extract a string where the following string may or may not exist</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293315#M88548</link>
      <description>&lt;P&gt;Thankyou.&lt;/P&gt;

&lt;P&gt;Yes, I discovered the requirements for no spaces  (apologies, my "sample" didn't convey that). I did play around with just using the space character too. I think I ill go home and start tomorrow with fresh eyes !&lt;/P&gt;

&lt;P&gt;Thankyou for the suggestions. You have started me on a couple of new paths of testing so much appreciated. I'll update here if I find a solution.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Oct 2017 05:38:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293315#M88548</guid>
      <dc:creator>rhysjones</dc:creator>
      <dc:date>2017-10-11T05:38:23Z</dc:date>
    </item>
    <item>
      <title>Re: Using regex to extract a string where the following string may or may not exist</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293316#M88549</link>
      <description>&lt;P&gt;Hi rhyjones,&lt;/P&gt;

&lt;P&gt;Are you trying to extract these fields using search query ie, rex command or doing it in transforms for index time?&lt;BR /&gt;
For search query, you can try below regex with rex command?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|rex field=FieldName "(?:Some Text 1\s+)(?P&amp;lt;myfield1&amp;gt;.+)(?=\s+Some Text 2|\",)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Ensure you have specified field=FieldName if your event data is not coming in _raw field, where FieldName is the name of the column/field in which the string to be extracted is present.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Oct 2017 10:03:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293316#M88549</guid>
      <dc:creator>jincy_18</dc:creator>
      <dc:date>2017-10-11T10:03:27Z</dc:date>
    </item>
    <item>
      <title>Re: Using regex to extract a string where the following string may or may not exist</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293317#M88550</link>
      <description>&lt;P&gt;Thankyou jincy_18. I will have a go when I get to the office tomorrow.&lt;/P&gt;

&lt;P&gt;I was experimenting using the rex command, but mostly in the field extraction wizard. Effectively I am only trying to extract "my field 1" and I am identifying it based on the fact it is preceded by the literal string "Some Text 1" and a space, and followed immediately by &lt;EM&gt;either&lt;/EM&gt; "Some Text 2" OR the ", combination.&lt;/P&gt;

&lt;P&gt;I discovered in another extract I was doing that in the event that  was immediately followed by the combination&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;","text3
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I had to use the following regex :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Some Text 1\s+(?P&amp;lt;my field 1&amp;gt;.+)\.{7}text3
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This kind of made me think I had a Unicode issue. &lt;/P&gt;

&lt;P&gt;Thankyou for the hint. I'll check it out tomorrow.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Oct 2017 10:36:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293317#M88550</guid>
      <dc:creator>rhysjones</dc:creator>
      <dc:date>2017-10-11T10:36:07Z</dc:date>
    </item>
    <item>
      <title>Re: Using regex to extract a string where the following string may or may not exist</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293318#M88551</link>
      <description>&lt;P&gt;Hi jincy_18,&lt;/P&gt;

&lt;P&gt;I did some more experimenting and unfortunately have the same issue. I can either extract "myfield1" when followed by ",&lt;BR /&gt;
or I can extract "myfield1" when followed by a space then a "Some Text 2".&lt;/P&gt;

&lt;P&gt;If I try to have both in a match group I get the one followed by ", extracted correctly, and all the other rows extract until they get to a ", combination.&lt;/P&gt;

&lt;P&gt;I might try a different tack.&lt;/P&gt;

&lt;P&gt;Thanks again.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Oct 2017 05:25:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293318#M88551</guid>
      <dc:creator>rhysjones</dc:creator>
      <dc:date>2017-10-12T05:25:12Z</dc:date>
    </item>
    <item>
      <title>Re: Using regex to extract a string where the following string may or may not exist</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293319#M88552</link>
      <description>&lt;P&gt;Hi rhys,&lt;/P&gt;

&lt;P&gt;Have you checked if the space characters are actually spaces or tabs?&lt;BR /&gt;
Also, in the sample you provided, " Some Text 1  Some Text 2", is " Some Text 1 " always present, I mean is it the same always, like wise for "Some Text 2" when ever it is present is it the same?&lt;/P&gt;</description>
      <pubDate>Thu, 12 Oct 2017 05:54:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293319#M88552</guid>
      <dc:creator>jincy_18</dc:creator>
      <dc:date>2017-10-12T05:54:56Z</dc:date>
    </item>
    <item>
      <title>Re: Using regex to extract a string where the following string may or may not exist</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293320#M88553</link>
      <description>&lt;P&gt;So effectively, I can get it running correctly with either "match" by themselves, but if I put them in a non-capturing match group, only the second match is "hit". That means items that are at the end of the line already are correctly returned, but items that have "Some Text 2" are actually captured all the way until the ", combination is matched.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Oct 2017 06:15:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293320#M88553</guid>
      <dc:creator>rhysjones</dc:creator>
      <dc:date>2017-10-12T06:15:23Z</dc:date>
    </item>
    <item>
      <title>Re: Using regex to extract a string where the following string may or may not exist</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293321#M88554</link>
      <description>&lt;P&gt;Hi jincy_18,&lt;/P&gt;

&lt;P&gt;Excellent question.&lt;/P&gt;

&lt;P&gt;"Some Text 1" is always there. This works for records that &lt;STRONG&gt;do&lt;/STRONG&gt; have text following the extracted field:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Some Text 1\s+(?P&amp;lt;my field 1&amp;gt;.+)\sSome Text 2
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This works for records that &lt;STRONG&gt;do not&lt;/STRONG&gt; have text following the extracted field:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Some Text 1\s+(?P&amp;lt;my field 1&amp;gt;.+)\",
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This does not work&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Some Text 1\s+(?P&amp;lt;my field 1&amp;gt;.+)(?:\sSome Text 2|\",)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This last one returns &lt;STRONG&gt;correct&lt;/STRONG&gt; extracts for records that &lt;STRONG&gt;do not&lt;/STRONG&gt; have text following the extracted field. For records that &lt;STRONG&gt;do&lt;/STRONG&gt; have text following the extracted field it returns all the following text up to the next instance of the ", combination rather than stopping before the "Some Text 2" literal string.&lt;/P&gt;

&lt;P&gt;Hope that makes sense. &lt;/P&gt;</description>
      <pubDate>Thu, 12 Oct 2017 10:31:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293321#M88554</guid>
      <dc:creator>rhysjones</dc:creator>
      <dc:date>2017-10-12T10:31:59Z</dc:date>
    </item>
    <item>
      <title>Re: Using regex to extract a string where the following string may or may not exist</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293322#M88555</link>
      <description>&lt;P&gt;What about:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Some Text 1\s+(?P&amp;lt;my field 1&amp;gt;.+?)(?:\sSome Text 2|\",)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Making the &lt;CODE&gt;.+&lt;/CODE&gt; a lazy match ( &lt;CODE&gt;.+?&lt;/CODE&gt; ) will help it to not include &lt;CODE&gt;Some Text 2&lt;/CODE&gt; as part of the match.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Oct 2017 15:29:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293322#M88555</guid>
      <dc:creator>cpetterborg</dc:creator>
      <dc:date>2017-10-12T15:29:57Z</dc:date>
    </item>
    <item>
      <title>Re: Using regex to extract a string where the following string may or may not exist</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293323#M88556</link>
      <description>&lt;P&gt;cpetterborg, that was the missing bit !! Thankyou !&lt;/P&gt;

&lt;P&gt;This now appears to be pulling the field in correctly in both cases.&lt;BR /&gt;
    Some Text 1\s+(?P&lt;MY field="" 1=""&gt;.+?)(?:\sSome Text 2|\",)&lt;/MY&gt;&lt;/P&gt;

&lt;P&gt;Thankyou both for all you assistance. Very much appreciated !&lt;/P&gt;</description>
      <pubDate>Thu, 12 Oct 2017 23:04:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-extract-a-string-where-the-following-string-may/m-p/293323#M88556</guid>
      <dc:creator>rhysjones</dc:creator>
      <dc:date>2017-10-12T23:04:39Z</dc:date>
    </item>
  </channel>
</rss>

