<?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 do you make a regex that skips an optional word? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-do-you-make-a-regex-that-skips-an-optional-word/m-p/436799#M124463</link>
    <description>&lt;P&gt;I'm fairly new to regex. In other languages, i just string split and hack it up as needed, but i'm trying to use regex w/ Splunk. I have 3 lines of text which I'll simplify for clarity.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;timestamp and Stuff stringIWantOne and optionalWord stringIWantThree extra words
timestamp and Stuff stringIWantOne and stringIWantTwo extra words
timestamp and Stuff stringIWantOne and optionalWord stringIWantThree extra words
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;In this example, i want to grab the actual content of "stringIWantOne" and "stringIWantThree" as well as "stringIWantTwo". &lt;/P&gt;

&lt;P&gt;stringIWantThree and Two are only ever 2 words. &lt;/P&gt;

&lt;P&gt;the regex string i have right now is: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Interface\ (?Psrc_interface&amp;gt;.*), changed state to (?:administratively) (?P&amp;lt;port_status&amp;gt;\w+)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If i take out the (?:administratively)  part, i almost get the desired results (up|down), but I also get "administratively". I want them to all be only "up | down" but I don't want to exclude the records that have the word administratively in it.&lt;/P&gt;</description>
    <pubDate>Thu, 13 Dec 2018 17:45:03 GMT</pubDate>
    <dc:creator>moorvogi</dc:creator>
    <dc:date>2018-12-13T17:45:03Z</dc:date>
    <item>
      <title>How do you make a regex that skips an optional word?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-you-make-a-regex-that-skips-an-optional-word/m-p/436799#M124463</link>
      <description>&lt;P&gt;I'm fairly new to regex. In other languages, i just string split and hack it up as needed, but i'm trying to use regex w/ Splunk. I have 3 lines of text which I'll simplify for clarity.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;timestamp and Stuff stringIWantOne and optionalWord stringIWantThree extra words
timestamp and Stuff stringIWantOne and stringIWantTwo extra words
timestamp and Stuff stringIWantOne and optionalWord stringIWantThree extra words
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;In this example, i want to grab the actual content of "stringIWantOne" and "stringIWantThree" as well as "stringIWantTwo". &lt;/P&gt;

&lt;P&gt;stringIWantThree and Two are only ever 2 words. &lt;/P&gt;

&lt;P&gt;the regex string i have right now is: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Interface\ (?Psrc_interface&amp;gt;.*), changed state to (?:administratively) (?P&amp;lt;port_status&amp;gt;\w+)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If i take out the (?:administratively)  part, i almost get the desired results (up|down), but I also get "administratively". I want them to all be only "up | down" but I don't want to exclude the records that have the word administratively in it.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Dec 2018 17:45:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-you-make-a-regex-that-skips-an-optional-word/m-p/436799#M124463</guid>
      <dc:creator>moorvogi</dc:creator>
      <dc:date>2018-12-13T17:45:03Z</dc:date>
    </item>
    <item>
      <title>Re: How do you make a regex that skips an optional word?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-you-make-a-regex-that-skips-an-optional-word/m-p/436800#M124464</link>
      <description>&lt;P&gt;This should do the trick:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Interface (?&amp;lt;src_interface&amp;gt;[^,]+), changed state to (administratively )?(?&amp;lt;port_status&amp;gt;\w+)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The '?' after the '(administratively )' capture group, basically tells the regex that the previous group/character is optional.&lt;/P&gt;

&lt;P&gt;Used this as my sample data:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Interface 123, changed state to administratively up
Interface 234, changed state to administratively down
Interface 321, changed state to up
Interface 432, changed state to down
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;It catches all 4 distinct values of "src_interface" and "port_status".&lt;/P&gt;

&lt;P&gt;I used &lt;A href="https://regex101.com/" target="_blank"&gt;regex101&lt;/A&gt; to help double check the regex.&lt;/P&gt;

&lt;P&gt;Hope this helps&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 22:28:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-you-make-a-regex-that-skips-an-optional-word/m-p/436800#M124464</guid>
      <dc:creator>aholzer</dc:creator>
      <dc:date>2020-09-29T22:28:35Z</dc:date>
    </item>
  </channel>
</rss>

