Splunk Search

Tricky behavior of escaping backslash in regex

Splunk Employee
Splunk Employee

Hey folks,

I am doing some regex stuff by rex command and find some tricky behavior.

Error: I tried to use \ to escape the backslash in three places. Not sure which one doesn't work.
alt text

But, this regex works on regex101.

OK, let's try some other ways in the search to make it accepted by my Splunk as below:
alt text

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.
\x5c can be a good replacement. But, why "\\" works to escape single \?

Any idea is appreciated.

Tags (3)

Ultra Champion

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.

To match a single \ in a string
you need \\ in your regex, to achieve that, you need \\\\ in the splunk search bar in the rex command.

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.

But I do agree that is a bit tricky and confusing, also because it doesn't seem to correspond to what is in documentation: http://docs.splunk.com/Documentation/Splunk/7.0.1/Search/SPLandregularexpressions

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...

PS: the code segment parser here on splunk answers also seems to struggle a bit with such backslash characters 😛


Thanks, I didn't realize I had to put in 4 backslashes for one.

0 Karma
Get Updates on the Splunk Community!

What’s new on Splunk Lantern in August

This month’s Splunk Lantern update gives you the low-down on all of the articles we’ve published over the past ...

Welcome to the Future of Data Search & Exploration

You have more data coming at you than ever before. Over the next five years, the total amount of digital data ...

This Week's Community Digest - Splunk Community Happenings [8.3.22]

Get the latest news and updates from the Splunk Community here! News From Splunk Answers ✍️ Splunk Answers is ...