<?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: Rex search for a specific pattern in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Rex-search-for-a-specific-pattern/m-p/689458#M234946</link>
    <description>&lt;P&gt;Try like this&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=prod_logs app_name="abc" 
| rex field=_raw "account_id\\\"\:\\\"(?&amp;lt;accid&amp;gt;[^\\]+)\\\"\,\\\"activity"
| where isnotnull (accid)
| table accid&lt;/LI-CODE&gt;</description>
    <pubDate>Mon, 03 Jun 2024 22:02:59 GMT</pubDate>
    <dc:creator>ITWhisperer</dc:creator>
    <dc:date>2024-06-03T22:02:59Z</dc:date>
    <item>
      <title>Rex search for a specific pattern</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Rex-search-for-a-specific-pattern/m-p/689413#M234932</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I have a splunk query that has following text in message field -&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;"message":"sypher:[tokenized] build successful -\xxxxy {\"data\":{\"account_id\":\"ABC123XYZ\",\"activity\":{\"time\":\"2024-05-31T12:37:25Z\}}"
&lt;BR /&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN class=""&gt;I&lt;/SPAN&gt;&amp;nbsp;need to extract value ABC123XYZ which is between&amp;nbsp;&lt;SPAN class=""&gt;account_id\":\"&lt;/SPAN&gt;&lt;SPAN class=""&gt;&amp;nbsp;and&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class=""&gt;\",\"activity&lt;/SPAN&gt;. I tried the following query&amp;nbsp;&lt;SPAN class=""&gt;but&lt;/SPAN&gt;&amp;nbsp;it's not returning any data.&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;index=prod_logs app_name="abc" 
| rex field=_raw "account_id\\\"\:\\\"(?&amp;lt;accid&amp;gt;[^\"]+)\\\"\,\\\"activity"
| where isnotnull (accid)
| table accid&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jun 2024 13:33:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Rex-search-for-a-specific-pattern/m-p/689413#M234932</guid>
      <dc:creator>nisheethbaxi</dc:creator>
      <dc:date>2024-06-03T13:33:56Z</dc:date>
    </item>
    <item>
      <title>Re: Rex search for a specific pattern</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Rex-search-for-a-specific-pattern/m-p/689415#M234934</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/264762"&gt;@nisheethbaxi&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;if you're sure to have the backslashes in your logs, you could try this regex:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| rex "account_id\\\":\\\"(?&amp;lt;account_id&amp;gt;[^\\]+)"&lt;/LI-CODE&gt;&lt;P&gt;that you can test at&amp;nbsp;&lt;A href="https://regex101.com/r/maaQBE/1" target="_blank"&gt;https://regex101.com/r/maaQBE/1&lt;/A&gt;&lt;/P&gt;&lt;P&gt;or the following (there's an issue using a regex in Spunk when there's backslash)&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| rex "account_id\\\\\":\\\\\"(?&amp;lt;account_id&amp;gt;[^\\]+)"&lt;/LI-CODE&gt;&lt;P&gt;Ciao.&lt;/P&gt;&lt;P&gt;Giuseppe&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jun 2024 13:57:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Rex-search-for-a-specific-pattern/m-p/689415#M234934</guid>
      <dc:creator>gcusello</dc:creator>
      <dc:date>2024-06-03T13:57:28Z</dc:date>
    </item>
    <item>
      <title>Re: Rex search for a specific pattern</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Rex-search-for-a-specific-pattern/m-p/689449#M234943</link>
      <description>&lt;P&gt;Tried both the expressions, Getting same error in both&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;regex 'account_id\\":\\"(?&amp;lt;account_id&amp;gt;[^\]+"activity)': Regex: missing terminating ] for character class.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jun 2024 19:41:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Rex-search-for-a-specific-pattern/m-p/689449#M234943</guid>
      <dc:creator>nisheethbaxi</dc:creator>
      <dc:date>2024-06-03T19:41:46Z</dc:date>
    </item>
    <item>
      <title>Re: Rex search for a specific pattern</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Rex-search-for-a-specific-pattern/m-p/689458#M234946</link>
      <description>&lt;P&gt;Try like this&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=prod_logs app_name="abc" 
| rex field=_raw "account_id\\\"\:\\\"(?&amp;lt;accid&amp;gt;[^\\]+)\\\"\,\\\"activity"
| where isnotnull (accid)
| table accid&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 03 Jun 2024 22:02:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Rex-search-for-a-specific-pattern/m-p/689458#M234946</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2024-06-03T22:02:59Z</dc:date>
    </item>
    <item>
      <title>Re: Rex search for a specific pattern</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Rex-search-for-a-specific-pattern/m-p/689486#M234948</link>
      <description>&lt;P&gt;Your data illustration strongly suggest that it is part of a JSON event like,&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;{"message":"sypher:[tokenized] build successful -\xxxxy {\"data\":{\"account_id\":\"ABC123XYZ\",\"activity\":{\"time\":\"2024-05-31T12:37:25Z\"}}", "some_field":"somevalue", "some_other_field": "morevalue"}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this case, Splunk should have given you a field named "message" &amp;nbsp;that has this value:&amp;nbsp;&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;"message":"sypher:[tokenized] build successful -\xxxxy {\"data\":{\"account_id\":\"ABC123XYZ\",\"activity\":{\"time\":\"2024-05-31T12:37:25Z\"}}"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What the developer is trying to do is to embed more data in this field, partially also in JSON. &amp;nbsp;For long-term maintainability, it is best not to treat that as text, either. &amp;nbsp;This means that regex is not the right tool for the job. &amp;nbsp;Instead, &amp;nbsp;try to get the embedded JSON first.&lt;/P&gt;&lt;P&gt;There is just one problem (in addition to missing a closing double quote for the &lt;U&gt;time&lt;/U&gt; value): the string&amp;nbsp;\xxxxy is illegal in JSON. &amp;nbsp;If this is the real data, Splunk would have bailed and NOT give you a field named "message". &amp;nbsp;In that case, you will have to deal with that first. &amp;nbsp;Let's explore how later.&lt;/P&gt;&lt;P&gt;For now, suppose your data is actually&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{"message":"sypher:[tokenized] build successful -\\\xxxxy {\"data\":{\"account_id\":\"ABC123XYZ\",\"activity\":{\"time\":\"2024-05-31T12:37:25Z\"}}", "some_field":"somevalue", "some_other_field": "morevalue"}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As such, Splunk would have given you a value for &lt;U&gt;message&lt;/U&gt; like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;sypher:[tokenized] build successful -\xxxxy {"data":{"account_id":"ABC123XYZ","activity":{"time":"2024-05-31T12:37:25Z"}}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Consequently, all you need to do is&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval jmessage = replace(message, "^[^{]+", "")
| spath input=jmessage&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You will get the following fields&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;data.account_id&lt;/TD&gt;&lt;TD&gt;data.activity.time&lt;/TD&gt;&lt;TD&gt;some_field&lt;/TD&gt;&lt;TD&gt;some_other_field&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ABC123XYZ&lt;/TD&gt;&lt;TD&gt;2024-05-31T12:37:25Z&lt;/TD&gt;&lt;TD&gt;somevalue&lt;/TD&gt;&lt;TD&gt;morevalue&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Here is an emulation of the "correct" data you can play with and compare with real data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval _raw = "{\"message\":\"sypher:[tokenized] build successful -\\\xxxxy {\\\"data\\\":{\\\"account_id\\\":\\\"ABC123XYZ\\\",\\\"activity\\\":{\\\"time\\\":\\\"2024-05-31T12:37:25Z\\\"}}\", \"some_field\":\"somevalue\", \"some_other_field\": \"morevalue\"}"
| spath
``` data emulation above ```&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now, if your raw data indeed contains \xxxxy inside a JSON block, you can still rectify that with text manipulation so you get a legal JSON. &amp;nbsp;But you have to tell your developer that they are logging bad JSON. (Recently there was a case where an IBM mainframe plugin sent Splunk bad data like this. &amp;nbsp;It is best for the developer to fix this kind of problem.)&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jun 2024 06:19:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Rex-search-for-a-specific-pattern/m-p/689486#M234948</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2024-06-04T06:19:34Z</dc:date>
    </item>
  </channel>
</rss>

