Splunk Search

regex not working propoerly

prannoy93singh
Engager

/api/v1/user/engines/forecast/xyz?abc=true

For the above URL, I want to display the URL in the table till xyz (/api/v1/user/engines/forecast/xyz).
I am using the following regex

| regex requestURI = _raw "/[^?]*/"                                // not include data after ?
| regex requestURI = ".*[^.txt]$"                                     // no .txt extensions in the URI
| regex requestURI = "/[0-9a-zA-Z]{3,}/"                         // minimum length should be 3

But the first one is not working and the whole URL is being displayed

Tags (1)
0 Karma
1 Solution

woodcock
Esteemed Legend

You are confusing regex with rex. Try this:

|makeresults | eval _raw="/api/v1/user/engines/forecast/xyz?abc=true"
| rex "^(?<requestURI1>/[^?]*/)"
| rex "(?<requestURI2>.*[^.txt])$"
| rex "(?<requestURI3>/[0-9a-zA-Z]{3,}/)"

View solution in original post

0 Karma

woodcock
Esteemed Legend

You are confusing regex with rex. Try this:

|makeresults | eval _raw="/api/v1/user/engines/forecast/xyz?abc=true"
| rex "^(?<requestURI1>/[^?]*/)"
| rex "(?<requestURI2>.*[^.txt])$"
| rex "(?<requestURI3>/[0-9a-zA-Z]{3,}/)"
0 Karma

MuS
SplunkTrust
SplunkTrust

Hi prannoy93singh,

to match / you need to escape them in the regex, try this:

 \/[^?]+

cheers, MuS

prannoy93singh
Engager

I tried running the above query, but it is still not giving the desired result.

/api/v1/user/abc/xyz/24966700::2?personalizationChannel=ADVISOR_CENTER

such queries are still showing

0 Karma

MuS
SplunkTrust
SplunkTrust

Use rex instead, test it with this run everywhere example:

| makeresults 
| eval foo="/api/v1/user/abc/xyz/24966700::2?personalizationChannel=ADVISOR_CENTER" 
| rex field=foo "(?<MyNewField>\/[^?]+)"

the field MyNewField has the value of /api/v1/user/abc/xyz/24966700::2 in this example.

cheers, MuS

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!

Build the Future of Agentic AI: Join the Splunk Agentic Ops Hackathon

AI is changing how teams investigate incidents, detect threats, automate workflows, and build intelligent ...

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