All Apps and Add-ons

Extracting field values using a negative match

mundus
Path Finder

I have log messages that look like the following:

Aug 4 11:13:57 craig gnome-keyring-daemon[2252]: PROMPT OUTPUT: [transport]

Aug 4 11:15:01 craig CRON[10865]: pam_unix(cron:session): session opened for user root by (uid=0)

Aug 4 11:25:51 craig sudo: craig : TTY=pts/5 ; PWD=/opt ; USER=root ; COMMAND=/bin/bash

Aug 4 08:57:56 craig xscreensaver: pam_unix(xscreensaver:auth): auth could not identify password for [craig]

I'm trying to extract:

gnome-keyring-daemon

CRON

sudo

xscreensaver

The regex generated by the field extraction tool is giving me "any character that is not a left bracket": [^\[]+

What I need is "any character that is not a left bracket or colon", such as [^\[|:]+ But if I put that in, the field extraction utility throws an error.

But for some reason Splunk doesn't like the standard "|" for OR. How can I write a field extraction that is the equivalent of "any character that is not a left bracket or colon"?

Thx.

Craig

0 Karma
1 Solution

gkanapathy
Splunk Employee
Splunk Employee

The pipe character is not used for alternation when specifying a character in a character class, i.e., what is inside of [] or [^] sets is not a regular expression, but listing sets of characters. This has nothing to do with Splunk, but is standard for most regex, including PCRE. Therefore, the correct character class specification is: [^\[:].

Also, I don't know why you have the extra backslash in your question. You need it if you're using rex in a search expression to quote the backslash, but when providing the regex directly, such as in the config file or the regex creator, it is wrong.

View solution in original post

0 Karma

gkanapathy
Splunk Employee
Splunk Employee

The pipe character is not used for alternation when specifying a character in a character class, i.e., what is inside of [] or [^] sets is not a regular expression, but listing sets of characters. This has nothing to do with Splunk, but is standard for most regex, including PCRE. Therefore, the correct character class specification is: [^\[:].

Also, I don't know why you have the extra backslash in your question. You need it if you're using rex in a search expression to quote the backslash, but when providing the regex directly, such as in the config file or the regex creator, it is wrong.

0 Karma

gkanapathy
Splunk Employee
Splunk Employee

Oh great. Splunkbase is adding the extra backslashes. Exactly what it should not be doing.

dmlee
Communicator

I think the regex is \w+\s\d+\s\d+:\d+:\d+\s\w+\s([^:[]+)

0 Karma

Ayn
Legend

The double backslashes are likely due to a bug on splunk-base that duplicates backslashes (see your own answer as well).

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

[Puzzles] Solve, Learn, Repeat: Character substitutions with Regular Expressions

This challenge was first posted on Slack #puzzles channelFor BORE at .conf23, we had a puzzle question which ...

Splunk Community Badges!

  Hey everyone! Ready to earn some serious bragging rights in the community? Along with our existing badges ...

[Puzzles] Solve, Learn, Repeat: Matching cron expressions

This puzzle (first published here) is based on matching timestamps to cron expressions.All the timestamps ...