<?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 to extract fields with different format in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Regex-to-extract-fields-with-different-format/m-p/291486#M88049</link>
    <description>&lt;P&gt;Hi nuaraujo,&lt;BR /&gt;
try this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| rex field=message    "^USER: (?P&amp;lt;username&amp;gt;.+?) (?P&amp;lt;operation&amp;gt;[A-Z]+ [A-Z]+) (?&amp;lt;comment&amp;gt;.*)"
| rex field=comment "- (?&amp;lt;id&amp;gt;\d+)\. (?&amp;lt;message&amp;gt;.*)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;In this way you have all fields.&lt;/P&gt;

&lt;P&gt;Bye.&lt;BR /&gt;
Giuseppe&lt;/P&gt;</description>
    <pubDate>Tue, 27 Mar 2018 09:20:32 GMT</pubDate>
    <dc:creator>gcusello</dc:creator>
    <dc:date>2018-03-27T09:20:32Z</dc:date>
    <item>
      <title>Regex to extract fields with different format</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Regex-to-extract-fields-with-different-format/m-p/291485#M88048</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;

&lt;P&gt;I need your help in order to get a regex that may extract fields from some messages.&lt;/P&gt;

&lt;P&gt;Example 1&lt;BR /&gt;
&lt;STRONG&gt;USER: user1 UPDATED CUSTOMER - 123456. Added new user. New user was added.&lt;/STRONG&gt;&lt;/P&gt;

&lt;P&gt;What I am looking at this message:&lt;BR /&gt;
&lt;STRONG&gt;username&lt;/STRONG&gt;: user1&lt;BR /&gt;
&lt;STRONG&gt;operation&lt;/STRONG&gt;: UPDATED CUSTOMER (always two words in uppercase) &lt;BR /&gt;
&lt;STRONG&gt;customer_id&lt;/STRONG&gt;: 123456 (always preceded by "-" and ending with ".") (not available in all messages)&lt;BR /&gt;
&lt;STRONG&gt;comment&lt;/STRONG&gt;: Added new user. New user was added &lt;/P&gt;

&lt;P&gt;Example2&lt;BR /&gt;
&lt;STRONG&gt;USER: user2 ADDED COUNTRY with identifier: Germany&lt;/STRONG&gt;&lt;/P&gt;

&lt;P&gt;What I am looking at this message:&lt;BR /&gt;
&lt;STRONG&gt;username&lt;/STRONG&gt;:user2&lt;BR /&gt;
&lt;STRONG&gt;operation&lt;/STRONG&gt;: ADDED COUNTRY (always two words in uppercase)&lt;BR /&gt;
&lt;STRONG&gt;comment&lt;/STRONG&gt;: with identifier: Germany (in this message I do not have customer_id field)&lt;/P&gt;

&lt;P&gt;I am using the following REGEX that I far from being accurate. It works for the first use case but not for the second &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; ... | rex field=message    "^USER: (?P&amp;lt;username&amp;gt;.+?) (?P&amp;lt;operation&amp;gt;[A-Z].+?) - (?P&amp;lt;id&amp;gt;.+?)\. (?P&amp;lt;message&amp;gt;.*)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;What I am looking for a final result:&lt;/STRONG&gt;&lt;BR /&gt;
|username....  | operation...........................| id...............| message................................................|&lt;BR /&gt;
|user1............|UPDATED CUSTOMER.....| 123456 ....| Added new user. New user was added |&lt;BR /&gt;
|user2............|ADDED COUNTRY............| ..................| with identifier: Germany.........................|&lt;/P&gt;

&lt;P&gt;Can someone help me, building a general regex, please?&lt;/P&gt;</description>
      <pubDate>Tue, 27 Mar 2018 09:05:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Regex-to-extract-fields-with-different-format/m-p/291485#M88048</guid>
      <dc:creator>nuaraujo</dc:creator>
      <dc:date>2018-03-27T09:05:57Z</dc:date>
    </item>
    <item>
      <title>Re: Regex to extract fields with different format</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Regex-to-extract-fields-with-different-format/m-p/291486#M88049</link>
      <description>&lt;P&gt;Hi nuaraujo,&lt;BR /&gt;
try this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| rex field=message    "^USER: (?P&amp;lt;username&amp;gt;.+?) (?P&amp;lt;operation&amp;gt;[A-Z]+ [A-Z]+) (?&amp;lt;comment&amp;gt;.*)"
| rex field=comment "- (?&amp;lt;id&amp;gt;\d+)\. (?&amp;lt;message&amp;gt;.*)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;In this way you have all fields.&lt;/P&gt;

&lt;P&gt;Bye.&lt;BR /&gt;
Giuseppe&lt;/P&gt;</description>
      <pubDate>Tue, 27 Mar 2018 09:20:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Regex-to-extract-fields-with-different-format/m-p/291486#M88049</guid>
      <dc:creator>gcusello</dc:creator>
      <dc:date>2018-03-27T09:20:32Z</dc:date>
    </item>
    <item>
      <title>Re: Regex to extract fields with different format</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Regex-to-extract-fields-with-different-format/m-p/291487#M88050</link>
      <description>&lt;P&gt;Can you try :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| rex field=message    "^USER: (?P&amp;lt;username&amp;gt;.+?) (?P&amp;lt;operation&amp;gt;\w+) (?P&amp;lt;comment&amp;gt;.*)" | rex field=comment "CUSTOMER - (?P&amp;lt;id&amp;gt;[^\.]+)"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Mar 2018 09:21:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Regex-to-extract-fields-with-different-format/m-p/291487#M88050</guid>
      <dc:creator>p_gurav</dc:creator>
      <dc:date>2018-03-27T09:21:14Z</dc:date>
    </item>
    <item>
      <title>Re: Regex to extract fields with different format</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Regex-to-extract-fields-with-different-format/m-p/291488#M88051</link>
      <description>&lt;P&gt;Hello @nuaraujo&lt;/P&gt;

&lt;P&gt;try something like this.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | rex field=message "USER:\s(?&amp;lt;username&amp;gt;.+?)\s(?&amp;lt;operation&amp;gt;\w+\s\w+)\s\-?\s(?&amp;lt;id&amp;gt;\d+)\.?\s(?&amp;lt;message&amp;gt;.*)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Hope it helps!&lt;/P&gt;</description>
      <pubDate>Tue, 27 Mar 2018 09:21:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Regex-to-extract-fields-with-different-format/m-p/291488#M88051</guid>
      <dc:creator>lloydknight</dc:creator>
      <dc:date>2018-03-27T09:21:46Z</dc:date>
    </item>
    <item>
      <title>Re: Regex to extract fields with different format</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Regex-to-extract-fields-with-different-format/m-p/291489#M88052</link>
      <description>&lt;P&gt;Hi @nuaraujo,&lt;/P&gt;

&lt;P&gt;Try the following regex instead. I've tested it on my lab with you two examples and it seems to be working fine. Note I am assuming your customer ID is a number so you might need to tweak that if that's not the case.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;^USER: (?P&amp;lt;username&amp;gt;\S+)\s+(?P&amp;lt;operation&amp;gt;[A-Z]+ [A-Z]+)(\s+\-\s+(?P&amp;lt;customerid&amp;gt;\d+)\.)?\s+(?P&amp;lt;message&amp;gt;.*)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
J&lt;/P&gt;</description>
      <pubDate>Tue, 27 Mar 2018 09:25:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Regex-to-extract-fields-with-different-format/m-p/291489#M88052</guid>
      <dc:creator>javiergn</dc:creator>
      <dc:date>2018-03-27T09:25:55Z</dc:date>
    </item>
    <item>
      <title>Re: Regex to extract fields with different format</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Regex-to-extract-fields-with-different-format/m-p/291490#M88053</link>
      <description>&lt;P&gt;Thank @p_gurav.&lt;/P&gt;

&lt;P&gt;Your suggestion would already be a good solution. However, can you just help me getting 2 words for "operation"? In your suggestion, I am only getting one. Even so, BIG THANK YOU for your quick reply.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Mar 2018 09:27:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Regex-to-extract-fields-with-different-format/m-p/291490#M88053</guid>
      <dc:creator>nuaraujo</dc:creator>
      <dc:date>2018-03-27T09:27:04Z</dc:date>
    </item>
    <item>
      <title>Re: Regex to extract fields with different format</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Regex-to-extract-fields-with-different-format/m-p/291491#M88054</link>
      <description>&lt;P&gt;Thanks&lt;BR /&gt;
Thanks&lt;BR /&gt;
Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Mar 2018 09:29:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Regex-to-extract-fields-with-different-format/m-p/291491#M88054</guid>
      <dc:creator>nuaraujo</dc:creator>
      <dc:date>2018-03-27T09:29:21Z</dc:date>
    </item>
  </channel>
</rss>

