<?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: Regex: I want to match a string and then extract the next lines until matching another string in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Regex-I-want-to-match-a-string-and-then-extract-the-next-lines/m-p/160948#M45526</link>
    <description>&lt;P&gt;HI edrivera3,&lt;BR /&gt;
the rex or regex is the best for that.try this to extract for example properties values and put them in one field:&lt;/P&gt;

&lt;P&gt;......| rex max_match=0 field=_raw " HERE YOU PUT YOUR REGEX"&lt;/P&gt;

&lt;P&gt;If you cannot easily write regex like me, use IFX,do as if you want to extract the values, the IFX will provide the regular expression that  can use there.&lt;/P&gt;</description>
    <pubDate>Mon, 28 Sep 2020 19:39:11 GMT</pubDate>
    <dc:creator>stephane_cyrill</dc:creator>
    <dc:date>2020-09-28T19:39:11Z</dc:date>
    <item>
      <title>Regex: I want to match a string and then extract the next lines until matching another string</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Regex-I-want-to-match-a-string-and-then-extract-the-next-lines/m-p/160947#M45525</link>
      <description>&lt;P&gt;HI&lt;/P&gt;

&lt;P&gt;I have the following in all my  events:&lt;/P&gt;

&lt;P&gt;ERROR=40392&lt;BR /&gt;
"This error ... blah...blah....&lt;BR /&gt;
... ... ..      ...  ... .. ...        ..... ..&lt;BR /&gt;
...  .. ...          ...  .. .          ..."&lt;BR /&gt;
END&lt;/P&gt;

&lt;P&gt;I would like to extract everything between "ERR0R=40302" and "END" in a field. Also the error number change for each event. I would appreciate your help.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2015 00:29:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Regex-I-want-to-match-a-string-and-then-extract-the-next-lines/m-p/160947#M45525</guid>
      <dc:creator>edrivera3</dc:creator>
      <dc:date>2015-04-21T00:29:09Z</dc:date>
    </item>
    <item>
      <title>Re: Regex: I want to match a string and then extract the next lines until matching another string</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Regex-I-want-to-match-a-string-and-then-extract-the-next-lines/m-p/160948#M45526</link>
      <description>&lt;P&gt;HI edrivera3,&lt;BR /&gt;
the rex or regex is the best for that.try this to extract for example properties values and put them in one field:&lt;/P&gt;

&lt;P&gt;......| rex max_match=0 field=_raw " HERE YOU PUT YOUR REGEX"&lt;/P&gt;

&lt;P&gt;If you cannot easily write regex like me, use IFX,do as if you want to extract the values, the IFX will provide the regular expression that  can use there.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 19:39:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Regex-I-want-to-match-a-string-and-then-extract-the-next-lines/m-p/160948#M45526</guid>
      <dc:creator>stephane_cyrill</dc:creator>
      <dc:date>2020-09-28T19:39:11Z</dc:date>
    </item>
    <item>
      <title>Re: Regex: I want to match a string and then extract the next lines until matching another string</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Regex-I-want-to-match-a-string-and-then-extract-the-next-lines/m-p/160949#M45527</link>
      <description>&lt;P&gt;For this sample log entry:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;ERROR=40392 "This error blah blah" END
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;It would be possible to use rex inline like (rex defaults to the field _raw unless you specify otherwise):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;your search&amp;gt; | rex "ERROR=\d+\s"(?&amp;lt;new_field&amp;gt;.+)"\sEND"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;You will end up with: new_field=This error blah blah&lt;/P&gt;

&lt;P&gt;You can put that into props.conf for a search time extraciton:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;EXTRACT-your_extract = ERROR=\d+\s"(?&amp;lt;new_field&amp;gt;.+)"\sEND
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Apr 2015 01:48:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Regex-I-want-to-match-a-string-and-then-extract-the-next-lines/m-p/160949#M45527</guid>
      <dc:creator>dflodstrom</dc:creator>
      <dc:date>2015-04-21T01:48:46Z</dc:date>
    </item>
    <item>
      <title>Re: Regex: I want to match a string and then extract the next lines until matching another string</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Regex-I-want-to-match-a-string-and-then-extract-the-next-lines/m-p/160950#M45528</link>
      <description>&lt;P&gt;edrivera3,&lt;/P&gt;

&lt;P&gt;First, let me recommend you check out regex101.com because it will show you exactly what your regex is capturing and what it's not. It also explains ever step of your regex. Very helpful for learning.&lt;/P&gt;

&lt;P&gt;Since you mention that the error will have "different numbers" I think it's worth pointing out that regex is a pattern matching. So sometimes you will notate literal things like &lt;CODE&gt;ERROR=&lt;/CODE&gt; and sometimes you will use representations like &lt;CODE&gt;\d&lt;/CODE&gt; for digit and &lt;CODE&gt;\d+&lt;/CODE&gt; for one or more digits. It helps to be precise when you can. So even if the numbers were different, if you always have a five digit error code the regex for just that... would look like this  &lt;CODE&gt;ERROR=\d{5}&lt;/CODE&gt;which translates to literally ERROR= followed by five digits... always. So in this case you represent what you don't want to capture, but you want to make sure is included which is: &lt;CODE&gt;ERROR=\d+\s+\"&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Then this could get tricky:  Your sample seems to have carriage returns. so while it might seem like a good idea to use a dot (which represents any character) and say &lt;CODE&gt;.+&lt;/CODE&gt; that would only work for one line in the message, since the dot actually represents any character except... newline, and it looks like you have newline...  so here's the trick.  there are flags that you can apply to the regex (See regex101 explanation)  for example prefix your regex with (?i) and that tells Splunk that you want the regex to be case &lt;EM&gt;insensitive&lt;/EM&gt;&lt;/P&gt;

&lt;P&gt;In this case you'll use the /s flag (another way to represent it... )   so to have the .+ include newline (and represent all characters including newline you code it like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;(?s)ERROR=\d+\s+\"(?P&amp;lt;myfield&amp;gt;.+)\"\s+END
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;which says: &lt;BR /&gt;
Look at this as if everything is a single line&lt;BR /&gt;
Walk past the following literal characters: ERROR=&lt;BR /&gt;
Then walk past one or more digits, followed by a space and a literal double quote&lt;BR /&gt;
Then create a field capturing group called "field"&lt;BR /&gt;
Inside the field you put &lt;EM&gt;one or more&lt;/EM&gt; characters&lt;BR /&gt;
But don't include the next double quote, the one or more spaces that follow or, the literal word END&lt;BR /&gt;
That last bit sort of anchors the field as &lt;EM&gt;before&lt;/EM&gt; the combination of double quote, spaces and END. Sometimes you have to be more specific than that... (if there are other things in the event that look very close to the rest) but it's fine here if that's really what it looks like.&lt;/P&gt;

&lt;P&gt;You can use that regex to extract a search time field (in the GUI, Settings&amp;gt; fields&amp;gt;extracted fields (and that will be placed into props.conf)&lt;BR /&gt;
Or you can use it for a rex in your search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;...|rex "(?s)ERROR=\d+\s+\"(?P&amp;lt;myfield&amp;gt;.+)\"\s+END"|HEAD 1|table myfield
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;In your research you may have come across something like &lt;CODE&gt;.*&lt;/CODE&gt; as well as &lt;CODE&gt;.+&lt;/CODE&gt;&lt;BR /&gt;
the &lt;CODE&gt;.*&lt;/CODE&gt; means zero or more characters and if it finds some it's very greedy, meaning it'll just keep going sometimes.&lt;BR /&gt;
the other means one or more, and it is perhaps less greedy... although still... greedy. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;
In this case, either is good... but you only use the * when you really need it. (or when you think you might have zero characters)&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2015 02:30:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Regex-I-want-to-match-a-string-and-then-extract-the-next-lines/m-p/160950#M45528</guid>
      <dc:creator>rsennett_splunk</dc:creator>
      <dc:date>2015-04-21T02:30:56Z</dc:date>
    </item>
    <item>
      <title>Re: Regex: I want to match a string and then extract the next lines until matching another string</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Regex-I-want-to-match-a-string-and-then-extract-the-next-lines/m-p/160951#M45529</link>
      <description>&lt;P&gt;For this sample log entry &lt;BR /&gt;
dkf:fhj fjff jffj from IP 11.11.111.11. jdjd"\n&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jul 2017 05:59:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Regex-I-want-to-match-a-string-and-then-extract-the-next-lines/m-p/160951#M45529</guid>
      <dc:creator>mohan401</dc:creator>
      <dc:date>2017-07-13T05:59:40Z</dc:date>
    </item>
  </channel>
</rss>

