Splunk Search

Splunk regex error: Missing terminating ] for character class

spluzer
Communicator

Hey Splunkers,

Noob. Trying to only retrieve the log names (ex. utility.log) after the last slash

blah\blah\blah\logs\error.log
blah\blah\blah\logs\audit.log
blah\blah\blah\logs\utility.log
blah\blah\blah\logs\service.log
blah\blah\blah\logs\servlet.log

Does anyone have any ideas as to why my regex returns the error below? Thanks all!

| rex field=source "\\\([^\\\]+)$"

Error in 'rex' command: Encountered the following error while compiling the regex '([^]+)$': Regex: missing terminating ] for character class.

P.S. The regex I am using above worked on regex 101

Tags (3)
0 Karma
1 Solution

gcusello
SplunkTrust
SplunkTrust

Hi spluzer,
try this

\\(?<log_name>\w+\.\w+)$

that you can test at https://regex101.com/r/kUsfJu/1

Bye.
Giuseppe

View solution in original post

gcusello
SplunkTrust
SplunkTrust

Hi spluzer,
try this

\\(?<log_name>\w+\.\w+)$

that you can test at https://regex101.com/r/kUsfJu/1

Bye.
Giuseppe

spluzer
Communicator

Sorry, I should have been more clear. I need to capture everything after the last slash for all 5 logs.

error.log
audit.log
utility.log
service.log
servlet.log

Moreover, I couldn't get what you sent (entering the log name in individually) to work in regex 101 or splunk.

Thanks!

0 Karma

gcusello
SplunkTrust
SplunkTrust

sorry, did you tried with

| rex field=source "\\(?<log_name>\w+\.\w+)$"

?
As you can see in regex101, it extracts all the requested filenames that are after tha last backslash.

Bye.
Giuseppe

0 Karma

spluzer
Communicator

Thanks. it required 3 slashes after the first quote.. and then it worked..thanks again! For some reason the 3rd slash (which I assume you posted) gets cut off when posting to the forum

| rex field=source "\(?\w+.\w+)$"

0 Karma

spluzer
Communicator

lol...it did it again !! THIS IS THE CORRECT ONE:

| rex field=source "\\(?\w+.\w+)$"

0 Karma

spluzer
Communicator

| rex field=source "\\(?\w+.\w+)$"

0 Karma

spluzer
Communicator

| rex field=source "\\(?\w+.\w+)$

0 Karma

spluzer
Communicator

lol...oh well i guess i cant post the correct code. it keeps getting overwritten during posting . anyway thanks Giuseppe. what you have is correct it just requires 3 slashes after the first quote

0 Karma

spluzer
Communicator
CORRECTION

Some text got cut out in posting
MY SPLUNK QUERY/REGEX IS THIS:

| rex field=source "\([^\]+)$"

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!

Where Innovation Takes Flight: The Splunk4Aviation Flight Sim Lands at .conf26

If you hear someone at .conf26 shouting "gear down, GEAR DOWN" across the show floor, you have found us.  The ...

Turn Cisco Telemetry Into Action with Cisco Data Fabric, powered by the Splunk ...

The surge in machine data is already hitting enterprise budgets, and the agentic era will only intensify it. ...

Persistent Queue at TcpOut — One of Splunk's Most Practical Features

Splunk introduced persistent queueing at the tcpout layer as one of the most practical resilience features in ...