<?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 How to use substr or regex to extract part of text in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-substr-or-regex-to-extract-part-of-text/m-p/710843#M240056</link>
    <description>&lt;P&gt;I have string like this ,&amp;nbsp;{"code":"1234","bday":"15-02-06T07:02:01.731+00:00" "name":"Alex", "role":"student","age":"16"}, and I want to extract role from this string. Can any one suggest way in splunk logs?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 06 Feb 2025 14:19:21 GMT</pubDate>
    <dc:creator>vvkarur</dc:creator>
    <dc:date>2025-02-06T14:19:21Z</dc:date>
    <item>
      <title>How to use substr or regex to extract part of text</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-substr-or-regex-to-extract-part-of-text/m-p/710843#M240056</link>
      <description>&lt;P&gt;I have string like this ,&amp;nbsp;{"code":"1234","bday":"15-02-06T07:02:01.731+00:00" "name":"Alex", "role":"student","age":"16"}, and I want to extract role from this string. Can any one suggest way in splunk logs?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2025 14:19:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-substr-or-regex-to-extract-part-of-text/m-p/710843#M240056</guid>
      <dc:creator>vvkarur</dc:creator>
      <dc:date>2025-02-06T14:19:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to use substr or regex to extract part of text</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-substr-or-regex-to-extract-part-of-text/m-p/710845#M240057</link>
      <description>&lt;P&gt;This looks like JSON so you should ingest it as such. Alternatively, you could use spath to extract the fields. Alternatively, look at the json functions.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2025 14:37:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-substr-or-regex-to-extract-part-of-text/m-p/710845#M240057</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2025-02-06T14:37:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to use substr or regex to extract part of text</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-substr-or-regex-to-extract-part-of-text/m-p/710860#M240062</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/276247"&gt;@vvkarur&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can use the rex field, like this example.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| rex field=_raw "\"role\"\:\"(?&amp;lt;role&amp;gt;[^,\"]+)\""&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2025 18:24:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-substr-or-regex-to-extract-part-of-text/m-p/710860#M240062</guid>
      <dc:creator>luizlimapg</dc:creator>
      <dc:date>2025-02-06T18:24:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to use substr or regex to extract part of text</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-substr-or-regex-to-extract-part-of-text/m-p/710890#M240068</link>
      <description>&lt;P&gt;As&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/225168"&gt;@ITWhisperer&lt;/a&gt;&amp;nbsp;points out, neither substring or regex is the correct tool to extract information from structured data such as JSON. &amp;nbsp;I assume that that so-called "string" is not the entire event because otherwise Splunk would have automatically extracted role at search time. &amp;nbsp;Suppose you have an event like this (I'm quite convinced that you missed a comma between '00"' and '"name'.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;stuff before ...  {"code":"1234","bday":"15-02-06T07:02:01.731+00:00", "name":"Alex", "role":"student","age":"16"} stuff after ...&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What you do is to use regex to extract the part that is compliant JSON (not a portion of it), then use spath or fromjson to extract all key-value pairs.&lt;/P&gt;&lt;P&gt;The following should usually work:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| rex "^[^{]*(?&amp;lt;json_message&amp;gt;{.+})"
| spath input=json_message&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That sample data will return&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;age&lt;/TD&gt;&lt;TD&gt;bday&lt;/TD&gt;&lt;TD&gt;code&lt;/TD&gt;&lt;TD&gt;json_message&lt;/TD&gt;&lt;TD&gt;name&lt;/TD&gt;&lt;TD&gt;role&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;16&lt;/TD&gt;&lt;TD&gt;15-02-06T07:02:01.731+00:00&lt;/TD&gt;&lt;TD&gt;1234&lt;/TD&gt;&lt;TD&gt;{"code":"1234","bday":"15-02-06T07:02:01.731+00:00", "name":"Alex", "role":"student","age":"16"}&lt;/TD&gt;&lt;TD&gt;Alex&lt;/TD&gt;&lt;TD&gt;student&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Here is an emulation for you to 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 = "stuff before ...  {\"code\":\"1234\",\"bday\":\"15-02-06T07:02:01.731+00:00\", \"name\":\"Alex\", \"role\":\"student\",\"age\":\"16\"} stuff after ..."
``` data emulation above ```&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2025 06:27:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-substr-or-regex-to-extract-part-of-text/m-p/710890#M240068</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2025-02-07T06:27:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to use substr or regex to extract part of text</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-substr-or-regex-to-extract-part-of-text/m-p/710908#M240072</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/276247"&gt;@vvkarur&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;&lt;BR /&gt;You can try this regex&lt;BR /&gt;&lt;BR /&gt;| rex field=_raw&amp;nbsp; "\"role\":\"(?&amp;lt;field_name&amp;gt;\w+)\""&lt;BR /&gt;&lt;BR /&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2025 11:34:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-substr-or-regex-to-extract-part-of-text/m-p/710908#M240072</guid>
      <dc:creator>smanojkumar</dc:creator>
      <dc:date>2025-02-07T11:34:21Z</dc:date>
    </item>
  </channel>
</rss>

