Splunk Search

understanding regex better

HattrickNZ
Motivator

I am trying to understand better how splunk regex works.

I have the below example:

This is a sample of the data I am trying to extract with Slot No.=1 being of interest:
...,measObjLdn="SGSN02KPR/Process:Process No.=5, Process type=GBP, Slot No.=1,...

Using splunks Interactive field extractor gives me this
(?i)=.*?, (?P\w+\s+\w+\.=\d+)(?=,)

if I want to look at these values in the search I can do this
`... | rex "(?i)=.*?, (?P\w+\s+\w+.=\d+)(?=,)" | stats values(FIELDNAME)

Now to understand this better I would like to use an example. What if I just want the values so instead of getting
Slot No.=1 I would get 1

So this (?i)=.*?, (?P\w+\s+\w+\.=\d+)(?=,) would go to ...?

Appreciate any help and trying to understand this better, or is there any documentation on this.

EDIT1
For instance this | rex ".*,.*Slot No.=(?P[^,]+)" will give me just the values this is just me playing around, I don't fully understand it.

Tags (2)
0 Karma
1 Solution

esix_splunk
Splunk Employee
Splunk Employee

Perhaps for you, its better to do a rex as follows, as you mentioned:

search.. | rex field=_raw "Slot No.\=(?<slot_number>[^,]+),"

This isnt a proper forum to learn regex, but this matches a "Slot No.=" string, and then uses a capture group for any thing that isnt a "," repeated infinite times till it finds a ",".

Key take away here is regex capture groups, and pattern matching.

View solution in original post

0 Karma

esix_splunk
Splunk Employee
Splunk Employee

Perhaps for you, its better to do a rex as follows, as you mentioned:

search.. | rex field=_raw "Slot No.\=(?<slot_number>[^,]+),"

This isnt a proper forum to learn regex, but this matches a "Slot No.=" string, and then uses a capture group for any thing that isnt a "," repeated infinite times till it finds a ",".

Key take away here is regex capture groups, and pattern matching.

0 Karma

HattrickNZ
Motivator

tks you have giving me food for thought!! but what is a good forum for regex is it stackoverflow?

0 Karma

ppablo
Retired

Hi @HattrickNZ

Just thought you might find this useful, but here's a previous answers post where a handful of users on Answers shared their favorite online (and 1 offline) regex resources. One of them has exercises for practice and better understanding.
http://answers.splunk.com/answers/153171/is-there-any-online-regex-tool-to-create-regular-e.html

0 Karma

jeffland
SplunkTrust
SplunkTrust

You can learn regex as you go I believe, you only really need help in truly complicated cases. What helps a lot is a tool like regex101 where you can try your expression on your sample data and which highlights your syntax graphically, explains your regex step for step and also has a quick reference of frequently used commands at hand - it's really great!

If you DO post regex code here, please keep in mind to post it as code - not with grave accents, but with an empty row before it and indented by four spaces. That will preserve any special characters such as angular brackets, which, as you can see, are lost in the question you posted.

0 Karma

HattrickNZ
Motivator

tks Jeff

Below posted using the code button above (

 )

"(?i)=.*?, (?P<FIELDNAME>w+s+w+.=d+)(?=,)"

Below posted using grave accents - Note the difference

"(?i)=.*?, (?Pw+s+w+.=d+)(?=,)"

0 Karma

esix_splunk
Splunk Employee
Splunk Employee

Regex, or regular expressions, are not a idea in only in the world of Splunk. These are used throughout the development world and have been for years. I would recommend you study a bit on PCRE based patterns to get a better understanding of how regular expressions fit into the world of Splunk.

www.regex101.com is quite a valuable resource, not to mention others out there..

0 Karma

HattrickNZ
Motivator

tks but would you help me understand and break this down?

"(?i)=.*?, (?P\w+\s+\w+.=\d+)(?=,)"

have to rewrite the above as everything between < and > symbols disappears, but not if i use &lt and &gt
"(?i)=.*?, (?P&ltFIELDNAME&gt\w+\s+\w+.=\d+)(?=,)"

&lt = <
&gt = >

&lt = <
&gt = >

0 Karma
Get Updates on the Splunk Community!

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...

Let’s Get You Certified – Vegas-Style at .conf24

Are you ready to level up your Splunk game? Then, let’s get you certified live at .conf24 – our annual user ...