<?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 build a regular expression that will split a field on the first underscore? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-build-a-regular-expression-that-will-split-a-field-on-the/m-p/250989#M74974</link>
    <description>&lt;PRE&gt;&lt;CODE&gt;(?P&amp;lt;field1&amp;gt;\S+)_(?P&amp;lt;field2&amp;gt;\w+)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 29 Nov 2016 18:49:07 GMT</pubDate>
    <dc:creator>sshelly_splunk</dc:creator>
    <dc:date>2016-11-29T18:49:07Z</dc:date>
    <item>
      <title>How to build a regular expression that will split a field on the first underscore?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-build-a-regular-expression-that-will-split-a-field-on-the/m-p/250987#M74972</link>
      <description>&lt;P&gt;I need to use regex to split a field into two parts, delimited by an underscore. &lt;/P&gt;

&lt;P&gt;The vast majority of the time, my field (a date/time ID) looks like this, where AB or ABC is a 2 or 3 character identifier.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;11232016-0056_ABC 
11232016-0056_AB
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I use the following rex command to extract, and it works great.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| rex field=originalField "(?&amp;lt;subField1&amp;gt;.*)\_(?&amp;lt;subField2&amp;gt;.*)" 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;For example:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;originalField = 11232016-0056_ABC
subField1 = 11232016-0056
subField2 = ABC
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;However, I have a few special cases where &lt;CODE&gt;originalField = 11232016-0056_ABC_M&lt;/CODE&gt;, where M could be anything alphanumeric following an additional underscore.&lt;/P&gt;

&lt;P&gt;When I use the above rex command, I get the following result:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;originalField = 11232016-0056_ABC_M
subField1 = 11232016-0056_ABC
subField2 = M
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I want to see the following:   &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;originalField = 11232016-0056_ABC_M
subField1 = 11232016-0056 
subField2 =  ABC_M
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Basically, I need it to split at the first underscore and ignore all subsequent underscores.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Nov 2016 18:28:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-build-a-regular-expression-that-will-split-a-field-on-the/m-p/250987#M74972</guid>
      <dc:creator>mstark31</dc:creator>
      <dc:date>2016-11-29T18:28:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to build a regular expression that will split a field on the first underscore?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-build-a-regular-expression-that-will-split-a-field-on-the/m-p/250988#M74973</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;.... | rex field=originalField "(?&amp;lt;subField1&amp;gt;[^_]+)_(?&amp;lt;subField2&amp;gt;.+)"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Nov 2016 18:47:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-build-a-regular-expression-that-will-split-a-field-on-the/m-p/250988#M74973</guid>
      <dc:creator>sundareshr</dc:creator>
      <dc:date>2016-11-29T18:47:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to build a regular expression that will split a field on the first underscore?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-build-a-regular-expression-that-will-split-a-field-on-the/m-p/250989#M74974</link>
      <description>&lt;PRE&gt;&lt;CODE&gt;(?P&amp;lt;field1&amp;gt;\S+)_(?P&amp;lt;field2&amp;gt;\w+)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Nov 2016 18:49:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-build-a-regular-expression-that-will-split-a-field-on-the/m-p/250989#M74974</guid>
      <dc:creator>sshelly_splunk</dc:creator>
      <dc:date>2016-11-29T18:49:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to build a regular expression that will split a field on the first underscore?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-build-a-regular-expression-that-will-split-a-field-on-the/m-p/250990#M74975</link>
      <description>&lt;P&gt;This works! Thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 29 Nov 2016 18:54:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-build-a-regular-expression-that-will-split-a-field-on-the/m-p/250990#M74975</guid>
      <dc:creator>mstark31</dc:creator>
      <dc:date>2016-11-29T18:54:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to build a regular expression that will split a field on the first underscore?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-build-a-regular-expression-that-will-split-a-field-on-the/m-p/250991#M74976</link>
      <description>&lt;P&gt;This still splits on the 2nd underscore.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Nov 2016 18:55:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-build-a-regular-expression-that-will-split-a-field-on-the/m-p/250991#M74976</guid>
      <dc:creator>mstark31</dc:creator>
      <dc:date>2016-11-29T18:55:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to build a regular expression that will split a field on the first underscore?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-build-a-regular-expression-that-will-split-a-field-on-the/m-p/250992#M74977</link>
      <description>&lt;P&gt;This should get you going.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;.... | rex field=originalField "(?&amp;lt;subField1&amp;gt;[^_]+)_(?&amp;lt;subField2&amp;gt;.*)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Use this if you want to keep the underscore at the end of the line in the case that the character is other than an underscore.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; .... | rex field=originalField "(?&amp;lt;subField1&amp;gt;.*?_)(?&amp;lt;subField2&amp;gt;.*)"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Nov 2016 19:37:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-build-a-regular-expression-that-will-split-a-field-on-the/m-p/250992#M74977</guid>
      <dc:creator>gdziuba</dc:creator>
      <dc:date>2016-11-29T19:37:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to build a regular expression that will split a field on the first underscore?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-build-a-regular-expression-that-will-split-a-field-on-the/m-p/250993#M74978</link>
      <description>&lt;PRE&gt;&lt;CODE&gt;| rex field=specimenId "(?&amp;lt;subField1&amp;gt;[^_]+)_(?&amp;lt;subField2&amp;gt;.*)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Changed + to * to account for cases where _ABC may not exist.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Nov 2016 19:50:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-build-a-regular-expression-that-will-split-a-field-on-the/m-p/250993#M74978</guid>
      <dc:creator>mstark31</dc:creator>
      <dc:date>2016-11-29T19:50:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to build a regular expression that will split a field on the first underscore?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-build-a-regular-expression-that-will-split-a-field-on-the/m-p/250994#M74979</link>
      <description>&lt;P&gt;sorry -too fast on the draw. I didnt see the additional info around possible 2nd "_"'s occurring. &lt;BR /&gt;
gdziuba's answer works perfectly (or so I think:))&lt;/P&gt;</description>
      <pubDate>Tue, 29 Nov 2016 20:38:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-build-a-regular-expression-that-will-split-a-field-on-the/m-p/250994#M74979</guid>
      <dc:creator>sshelly_splunk</dc:creator>
      <dc:date>2016-11-29T20:38:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to build a regular expression that will split a field on the first underscore?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-build-a-regular-expression-that-will-split-a-field-on-the/m-p/250995#M74980</link>
      <description>&lt;P&gt;Hello Past mstark31. Current mstark31 thanks you for asking this question 3 years ago.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 13:46:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-build-a-regular-expression-that-will-split-a-field-on-the/m-p/250995#M74980</guid>
      <dc:creator>mstark31</dc:creator>
      <dc:date>2019-12-13T13:46:35Z</dc:date>
    </item>
  </channel>
</rss>

