<?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 Tricky behavior of escaping backslash in regex in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Tricky-behavior-of-escaping-backslash-in-regex/m-p/321295#M95984</link>
    <description>&lt;P&gt;Hey folks, &lt;/P&gt;

&lt;P&gt;I am doing some regex stuff by rex command and find some tricky behavior. &lt;/P&gt;

&lt;P&gt;Error: I tried to use \ to escape the backslash in three places. Not sure which one doesn't work. &lt;BR /&gt;
&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/4474i98C2DA4D33D306E9/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;But, this regex works on regex101. &lt;/P&gt;

&lt;P&gt;OK, let's try some other ways in the search to make it accepted by my Splunk as below: &lt;BR /&gt;
&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/4475iB562B3E2C11904BE/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;This time, it works perfectly. Here is the confusing point. Why does "\" work at the beginning, but doesn't work for the second and third backslash. &lt;BR /&gt;
&lt;CODE&gt;\x5c&lt;/CODE&gt; can be a good replacement. But, why "\\" works to escape single &lt;CODE&gt;\&lt;/CODE&gt;?&lt;/P&gt;

&lt;P&gt;Any idea is appreciated. &lt;/P&gt;</description>
    <pubDate>Wed, 07 Mar 2018 11:45:48 GMT</pubDate>
    <dc:creator>ypeng_splunk</dc:creator>
    <dc:date>2018-03-07T11:45:48Z</dc:date>
    <item>
      <title>Tricky behavior of escaping backslash in regex</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Tricky-behavior-of-escaping-backslash-in-regex/m-p/321295#M95984</link>
      <description>&lt;P&gt;Hey folks, &lt;/P&gt;

&lt;P&gt;I am doing some regex stuff by rex command and find some tricky behavior. &lt;/P&gt;

&lt;P&gt;Error: I tried to use \ to escape the backslash in three places. Not sure which one doesn't work. &lt;BR /&gt;
&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/4474i98C2DA4D33D306E9/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;But, this regex works on regex101. &lt;/P&gt;

&lt;P&gt;OK, let's try some other ways in the search to make it accepted by my Splunk as below: &lt;BR /&gt;
&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/4475iB562B3E2C11904BE/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;This time, it works perfectly. Here is the confusing point. Why does "\" work at the beginning, but doesn't work for the second and third backslash. &lt;BR /&gt;
&lt;CODE&gt;\x5c&lt;/CODE&gt; can be a good replacement. But, why "\\" works to escape single &lt;CODE&gt;\&lt;/CODE&gt;?&lt;/P&gt;

&lt;P&gt;Any idea is appreciated. &lt;/P&gt;</description>
      <pubDate>Wed, 07 Mar 2018 11:45:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Tricky-behavior-of-escaping-backslash-in-regex/m-p/321295#M95984</guid>
      <dc:creator>ypeng_splunk</dc:creator>
      <dc:date>2018-03-07T11:45:48Z</dc:date>
    </item>
    <item>
      <title>Re: Tricky behavior of escaping backslash in regex</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Tricky-behavior-of-escaping-backslash-in-regex/m-p/321296#M95985</link>
      <description>&lt;P&gt;As far as I'm aware, there is some double escaping going on, first from the search bar to the regex and then of course inside the regex.&lt;/P&gt;

&lt;P&gt;To match a single &lt;CODE&gt;\&lt;/CODE&gt; in a string&lt;BR /&gt;
you need &lt;CODE&gt;\\&lt;/CODE&gt; in your regex, to achieve that, you need &lt;CODE&gt;\\\\&lt;/CODE&gt; in the splunk search bar in the rex command.&lt;/P&gt;

&lt;P&gt;The reason your second attempt seems to work is that you do not require splunk to match the full string from the start, so Splunk is not matching both backslashes at the start of the path, but ignores the first and then starts the match from the second backslash.&lt;/P&gt;

&lt;P&gt;But I do agree that is a bit tricky and confusing, also because it doesn't seem to correspond to what is in documentation: &lt;A href="http://docs.splunk.com/Documentation/Splunk/7.0.1/Search/SPLandregularexpressions"&gt;http://docs.splunk.com/Documentation/Splunk/7.0.1/Search/SPLandregularexpressions&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Also somehow your eval requires 4 backslashes to make the field start with 2 backslashes but no additional backslashes for the backslashes in the middle of your string...&lt;/P&gt;

&lt;P&gt;PS: the code segment parser here on splunk answers also seems to struggle a bit with such backslash characters &lt;span class="lia-unicode-emoji" title=":face_with_tongue:"&gt;😛&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Mar 2018 15:08:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Tricky-behavior-of-escaping-backslash-in-regex/m-p/321296#M95985</guid>
      <dc:creator>FrankVl</dc:creator>
      <dc:date>2018-03-07T15:08:30Z</dc:date>
    </item>
    <item>
      <title>Re: Tricky behavior of escaping backslash in regex</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Tricky-behavior-of-escaping-backslash-in-regex/m-p/321297#M95986</link>
      <description>&lt;P&gt;Thanks, I didn't realize I had to put in 4 backslashes for one. &lt;/P&gt;</description>
      <pubDate>Thu, 24 May 2018 18:43:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Tricky-behavior-of-escaping-backslash-in-regex/m-p/321297#M95986</guid>
      <dc:creator>ifeldshteyn</dc:creator>
      <dc:date>2018-05-24T18:43:12Z</dc:date>
    </item>
  </channel>
</rss>

